美文网首页
wordpress根据分类别名获取分类ID

wordpress根据分类别名获取分类ID

作者: ppmoon | 来源:发表于2016-08-22 20:58 被阅读640次

在制作wordpress模板的时候可能会有使用分类别名获取分类ID的情况,比如有些函数不接受分类别名只能通过ID读取。

get_category_by_slug('分类别名'); 

通过上面的函数可以通过分类名称获得整个分类的数组信息。
我们将获得的组数信息打印出来。

WP_Term Object ( 
[term_id] => 15 
[name] => 专家 
[slug] => expert 
[term_group] => 0 
[term_taxonomy_id] => 15 
[taxonomy] => category 
[description] => [parent] => 14 
[count] => 1 
[filter] => raw 
[cat_ID] => 15 
[category_count] => 1 
[category_description] => [cat_name] => 专家 
[category_nicename] => expert 
[category_parent] => 14 )

将数组赋值给一个变量比如

$which_cat=get_category_by_slug('分类别名'); 

然后通过调用数组内部数据的方法获取分类ID

$which_cat->term_id;

相关文章

网友评论

      本文标题:wordpress根据分类别名获取分类ID

      本文链接:https://www.haomeiwen.com/subject/pisgsttx.html