Reputation: 12438
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)
i.e. 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') .'&layout=standard&show-faces=true&width=450&action=like&font=arial&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';
return $content . $bio_box;
}
add_action("the_content", "author_bio_display");
Upvotes: 0
Views: 5220
Reputation: 11699
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' );
Upvotes: 1
Reputation: 2167
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*') .'&layout=standard&show-faces=true&width=450&action=like&font=arial&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';
return $content . $bio_box;
Upvotes: 0
Reputation: 5905
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.
Upvotes: 2
Reputation: 3622
If you are currently on a detail page i.e 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*') .'&layout=standard&show-faces=true&width=450&action=like&font=arial&colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" id="facebook-like"></iframe>';
return $content . $bio_box;
}
Upvotes: 0