WordPress根据文章ID获取文章所在分类函数:wp_get_post_categories
WordPress函数wp_get_post_categories根据文章的ID获取该文章所在分类,如果文章分…
WordPress函数wp_get_post_categories根据文章的ID获取该文章所在分类,如果文章分配了多个分类,则返回所有分类的ID。
wp_get_post_categories( int $post_id, array $args = array() )
函数参数
$post_id
整数
文章的ID
$args
数组
分类查询参数,可以参考get_categories()函数的参数说明。
函数返回值
返回一个数组,包含该文章所在的所有分类ID,例如:
Array
(
    [0] => 1
    [1] => 35
    [2] => 3
)函数使用示例
$post_categories = wp_get_post_categories($post_id);
foreach($post_categories as $c){
	$cat = get_category($c);
	echo '<li><a href="' . esc_url(get_category_link($cat->term_id)) . '">' . $cat->name . '</a></li>';
}扩展阅读
wp_get_post_categories()函数位于:wp-includes/post.php
相关函数:
- wp_get_object_terms()
 - wp_get_post_terms()
 - wp_get_post_cats()
 - wp_get_post_tags()
 - wp_set_post_categories()
 - get_category()
 
类别:WordPress函数讲解、 
		本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!