wordpress做的文章系統(tǒng),361源碼想實(shí)現(xiàn)這樣的一個小功能:調(diào)用文章的tags標(biāo)簽作為文章頁面的keywords。可是由于標(biāo)簽調(diào)用函數(shù)默認(rèn)帶超鏈接,因此使用其作為文章頁面的關(guān)鍵詞前需去掉超鏈接變身為純文本,否則頁面會出現(xiàn)錯位等情況。
wordpress調(diào)用不帶超鏈接的tag標(biāo)簽的方法其實(shí)網(wǎng)上已有人公布,通過wordpress強(qiáng)大的functions.php文件即可實(shí)現(xiàn)。
下面是調(diào)用純文本Tag標(biāo)簽的實(shí)現(xiàn)辦法:
在當(dāng)前WordPress主題的functions.php文件中添加以下函數(shù)代碼(所有標(biāo)簽,用英文逗號分割):
function tagtext(){ global $post; $gettags = get_the_tags($post->ID); if ($gettags) { foreach ($gettags as $tag) { $posttag[] = $tag->name; } $tags = implode( ',', $posttag ); echo $tags; } }在要顯示純文本標(biāo)簽的地方添加以下調(diào)用代碼:
<?php tagtext();?>
1、如果只是需要獲取第一個純文本標(biāo)簽:
<?php$posttags = get_the_tags();$count=0;if ($posttags) { foreach($posttags as $tag) { $count++; if (1 == $count) { echo $tag->name . '|'; } }}?>2、下面的代碼也可以獲取文章第一個標(biāo)簽(純文本):
<?php $tag = get_the_tags($id); if ($tag) { $tag = $tag[0]; echo $tag->name; }?>3、如果希望獲取wordpress的第一個標(biāo)簽鏈接(帶鏈接):
//文章第一個tagfunction 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(); ?>
這樣361源碼就把幾種經(jīng)常用的調(diào)用tag標(biāo)簽的情況都給大家做了介紹,希望能對你的使用有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選