作wordpress模板时为了实现一些功能,常需要在分类列表页面或文章页面调用当前所属分类的ID,获取方法很简单,几行代码即可实现。
文章页面:
12$category = get_the_category(); //the_category()是文章当前所属分类函数,把分类信息传递给变量$categoryecho $category[0]->cat_ID; //输入数组中的分类ID
函数返回值参考:
cat_ID 分类ID
cat_name 分类名称
category_nicename 分类别名
category_description 分类描述
category_parent 父分类ID
category_count 分类下的文章数量
分类列表页面:
123global $wp_query;$cat_id = get_query_var('cat');echo $cat_id; //输入分类ID
get_query_var()函数位于wp-includes/query.php
1234function get_query_var($var) { global $wp_query; return $wp_query->get($var);}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。