想完成读取文章内容的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(); ?>