WordPress获取Tag信息标签:get_the_tags

本站正在使用的主机

本站目前托管于腾讯云香港轻量服务器,价格便宜访问速度也快,使用本站推荐链接购买还可免费获得WordPress运行环境优化服务。

最新活动 »

WordPress模板标签get_the_tags用于获取标签信息,包括标签ID、别名、名称、描述等。get_the_tags()函数需要用在The Loop主循环中,如果在主循环之外使用,需要传递文章ID。

get_the_tags( int $id = 0 )

函数参数

$id

整数型,默认值:当前文章ID

指定文章ID,将返回该文章的标签信息。

函数返回值

Array (
	[0] => WP_Term Object (
		[term_id] => 27
		[name] => CDN
		[slug] => cdn
		[term_group] => 0
		[term_taxonomy_id] => 27
		[taxonomy] => post_tag
		[description] => CDN的全称是Content Delivery Network,即内容分发网络。
		[parent] => 0
		[count] => 11
		[filter] => raw
	)
)

函数使用示例

<?php
	$posttags = get_the_tags();
	if ($posttags) {
	  foreach($posttags as $tag) {
		echo '标签ID:' . $tag->term_id;
		echo '<br />标签名称:' . $tag->name;
		echo '<br />标签描述:' . $tag->description;
	  }
	}
?>

扩展阅读

get_the_tags()函数位于:wp-includes/category-template.php

相关函数:

背字根微信二维码

本站文章均为原创,码字非常不容易,转载请注明原文出处,给苦逼的作者保留一点创作动力。