WordPress获取文章所属分类名称、分类ID、分类别名

在我们实际开发 wordpress 主题过程中,可能会经常用到获取当前分类名称、分类 ID或者是分类别名。

下面分享几个获取文章所属分类、ID、别名的wordpress代码案例。

wordpree自带函数

//获取分类名称和分类链接
<?php the_category(); ?>

但是在很多时候我们在开发wordpress 主题过程中并不需要用到带分类链接的分类名称,这时候以下几种获取方式便可以用到。

通用页面:

$category = get_the_category(); //the_category()是文章当前所属分类函数,把分类信息传递给变量$category
echo $category[0]->cat_ID; //输入数组中的分类ID

分类页面:

global $wp_query;
$cat_id = get_query_var(\'cat\');
echo $cat_id; //输入分类ID

在主题functions.php添加自定义函数,然后再调用这个函数。

function get_current_category_id() {
$current_category = single_cat_title(\'\', false);//获得当前分类目录名称
return get_cat_ID($current_category);//获得当前分类目录 ID
}

获得当前分类目录 ID:

<?php echo get_current_category_id(); ?>

函数返回值参考:

cat_ID 分类ID
cat_name 分类名称
category_nicename 分类别名
category_description 分类描述
category_parent 父分类ID
category_count 分类下的文章数量

文章来自WE7资源网www.we7cc.cn转载请保留本链接,谢谢!

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

源博士 WordPress教程 WordPress获取文章所属分类名称、分类ID、分类别名 https://www.yub4.cn/wordpress%e8%8e%b7%e5%8f%96%e6%96%87%e7%ab%a0%e6%89%80%e5%b1%9e%e5%88%86%e7%b1%bb%e5%90%8d%e7%a7%b0%e3%80%81%e5%88%86%e7%b1%bbid%e3%80%81%e5%88%86%e7%b1%bb%e5%88%ab%e5%90%8d/

常见问题

相关文章

官方客服团队

为您解决烦忧 - 24小时在线 专业服务

WordPress获取文章所属分类名称、分类ID、分类别名-海报

分享本文封面