簡體   English   中英

獲得WordPress文章的永久鏈接

[英]Getting the permalink to the post in wordpress

我試圖在我的博客上的每個帖子上添加facebook like按鈕。 我已經設法得到了我需要的任何東西來添加類似的按鈕,我唯一需要的是,如何訪問函數author_bio_display($content)的當前帖子的鏈接,即在它所說的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"); 

要獲取當前ID而不創建全局$ post變量:

$id = get_the_id();

get_permalink($id);

大多數循環外函數以“get_”開頭,這些函數不會回顯,而是返回數據。

首先, 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' );

如果您當前在詳細信息頁面上,即single.php ,我已經定義了一個變量$post並在$permalink保存當前的POST-> ID。現在您可以使用它。

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

你需要做..

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM