需要獲取當前文章不帶超級鏈接的標簽,這一節的wordpress開發教程就來學習下怎么獲取不帶鏈接的標簽吧
在開發插件的SEO功能時有這個需求,需要獲取當前文章不帶超級鏈接的標簽,這一節的wordpress開發教程就來學習下怎么獲取不帶鏈接的標簽吧。
- 參考來源:ztmao.com/jiaocheng/2775.html

核心代碼:
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(); ?>
怎么在日志循環外獲取日志的ID呢?來看看這篇wordpress開發教程吧。