简体   繁体   中英

Getting the permalink to the post in wordpress

I am trying to append the facebook like button to each of the post on my blog. I have managed to get whatever I need to add the like button, the only thing I need is, how can I access the current post's link inside the function author_bio_display($content) ie at the place where it says rawurlencode('post permalink goes here') ?

function author_bio_display($content)  
{  
        $bio_box = '<iframe src="http://www.facebook.com/plugins/like.php?href='. rawurlencode('post permalink goes here') .'&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';

        return $content . $bio_box;  
}  

add_action("the_content", "author_bio_display"); 

To get the current ID without making a global $post variable:

$id = get_the_id();

And

get_permalink($id);

Most out-of-loop functions begin with "get_" these functions do not echo but return data instead.

First thing is that the_content is not an Action Hook , its a Filter Hook so you should use add_filter instead of add_action .

function attach_like_button($content) {
  $post_id = $GLOBALS['post']->ID;
  $permalink = get_permalink($post_id);
  $link_button = ''; // Get latest facebook like button code from  https://developers.facebook.com/docs/reference/plugins/like/
  return $content.$link_button;
}

add_filter( 'the_content', 'attach_like_button' );

If you are currently on a detail page ie single.php here I have defined a variable $post and save current POST->ID in $permalink .Now you can play with it.

function author_bio_display($content)  
{  

        global $post;
        $permalink = get_permalink($post->ID);
        $bio_box = '<iframe src="http://www.facebook.com/plugins/like.php?href='. rawurlencode('*post permalink*') .'&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';

        return $content . $bio_box;  
} 

You need to do..

  $bio_box_id = get_permalink($post->ID);
  $bio_box = '<iframe src="http://www.facebook.com/plugins/like.php?href='. rawurlencode('*post permalink*') .'&amp;layout=standard&amp;show-faces=true&amp;width=450&amp;action=like&amp;font=arial&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';
  return $content . $bio_box;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM