rozanchetta

[WordPress] [Post-formats] Grab first post link

Jun 13th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.85 KB | None | 0 0
  1. <?php
  2. // Busca o primeiro link do post para o formato LINK. Se não houver link, traz o título do post
  3.  
  4. // Colar no arquivo functions.php
  5.  
  6. function temabase_get_the_post_format_url() {
  7.     global $post, $posts;
  8.     $first_link = '';
  9.     ob_start();
  10.     ob_end_clean();
  11.     $output = preg_match_all('/<a[\s]+[^>]*?href[\s]?=[\s\"\']+(.*?)[\"\']+.*?>([^<]+|.*?)?<\/a>/is', $post->post_content, $matches);
  12.     if (empty($matches[0]))
  13.         return the_title('<h2 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h2>');
  14.     $first_link = $matches [0] [0];
  15.     return '<h2 class="entry-title link">' . $first_link . '</h2>';
  16. }
  17.  
  18. // Susbstitue o título do post - colar no arquivo content-link.php
  19.  
  20. if (is_single()):  the_title('<h1 class="entry-title">', '</h1>');
  21. else: echo temabase_get_the_post_format_url();
  22.  endif;
Advertisement
Add Comment
Please, Sign In to add comment