女武神的终末第三季免费观看,国产精品va无码二区,欧美日韩在线视频观看,国产在线观看网站

wordpress調用不帶超鏈接的Tag標簽

2017-09-23 wordpress教程
  • 文章介紹
  • 快速入門
  • 評價&建議

想實現調用文章的tags標簽作為文章頁面的keywords,可由于標簽調用函數默認帶超鏈接,因此使用其作為文章頁面的關鍵詞前需去掉超鏈接變身為純文本,否則頁面會出現錯位等情況。wordpress調用不帶超鏈接的tag標簽的方法已有人公布,通過wordpress強大的functions.php文件即可實現。

調用純文本Tag標簽:

在當前WordPress主題的functions.php文件中添加以下函數代碼(所有標簽,用英文逗號分割):

  • function tagtext(){ global $post; $gettags = get_the_tags($post->ID); if ($gettags) { foreach ($gettags as $tag) { $posttag[] = $tag->name; } $tags = implode( ',', $posttag ); echo $tags; } }

在要顯示純文本標簽的地方添加以下調用代碼:

  • <?php tagtext();?>

如果只是需要獲取第一個純文本標簽:

  • <?php
  • $posttags = get_the_tags();
  • $count=0;
  • if ($posttags) {
  • ? foreach($posttags as $tag) {
  • ??? $count++;
  • ??? if (1 == $count) {
  • ????? echo $tag->name . '|';
  • ??? }
  • ? }
  • }
  • ?>

下面的代碼也可以獲取文章第一個標簽(純文本):

  • <?php
  • ??? $tag = get_the_tags($id);
  • ??????? if ($tag) {
  • ??? $tag = $tag[0];
  • ??? echo $tag->name;
  • ??? }
  • ?>

如果希望獲取wordpress的第一個標簽鏈接(帶鏈接):

  • //文章第一個tag
  • function first_tag_link()
  • {
  • ??? if ( $posttags = get_the_tags() )
  • ??? {
  • ??????? $tag = current( $posttags );
  • ??????? printf(
  • ??????????? '<a href="%1$s"><span>%2$s</span></a>',
  • ??????????? get_tag_link( $tag->term_id ),
  • ??????????? esc_html( $tag->name )
  • ???????? );
  • ??? }
  • }

用法:

  • <?php first_tag_link(); ?>
1 0

企業建站推薦正版商業主題,國內專業團隊開發,完善售后,是您不二選擇。

正版主題商店

主題貓WP建站,累計幫助1300+客戶成功建站,為站長提供支持!

立刻開啟你的建站之旅
QQ在線客服

服務熱線

wordpress建站咨詢