2013-09-01 3 views
0

Я пытаюсь добавить фейсбук как кнопка к каждому сообщению в своем блоге. Мне удалось получить все, что мне нужно, чтобы добавить понравившуюся кнопку, единственное, что мне нужно, как я могу получить доступ к ссылке текущего сообщения внутри функции author_bio_display($content) i.e в том месте, где говорится: rawurlencode('post permalink goes here')?Получение постоянной ссылки на сообщение в Wordpress

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"); 

ответ

0

Если вы в настоящее время на странице сведений single.php здесь т.е. я определил переменную $post и сохранить текущую post-> ID в $permalink .Теперь вы можете играть с ним.

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; 
} 
1

Чтобы получить текущий идентификатор без глобального $ сообщения переменного:

$id = get_the_id(); 

И

get_permalink($id); 

Большинства вне цикла функции начинается с «get_» эти функции не эхо, а возвращать данные.

0

Вам нужно сделать ..

$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; 
1

Первое, что the_content не является Action Hook, сво Filter Hook поэтому вы должны использовать add_filter вместо 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'); 
Смежные вопросы