一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

wordpress中取得当前分类或父分类id程序代码

时间:2013-02-28 编辑:简简单单 来源:一聚教程网

具体

获取当前分类:

 代码如下 复制代码


我们可以在得到这个分类ID的基础上,自定义一个函数,求根分类的ID:

 代码如下 复制代码

function get_category_root_id($cat)
{
$this_category = get_category($cat); // 取得当前分类
while($this_category->category_parent) // 若当前分类有上级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
}
return $this_category->term_id; // 返回根分类的id号
}

最后,当前文章的根分类就可以用函数get_category_root_id(the_category_ID(false))来表示。


另外再分享一个分类相关的热门文章 利用Popularity Contest插件来实例

个插件有2个function可用

akpc_most_popular_in_cat和akpc_most_popular,分别是指定一个分类id或取当前分类和所有的排名。

但是akpc_most_popular_in_cat有一点问题,就是原来的sql语句是在show_top_ranked_in_cat里面的sql是

 代码如下 复制代码

WHERE tt.term_id = ‘”.intval($cat_ID).”‘

这样,只能在一个分类里面查找,需要修改成支持多个分类id的。改成如下

 代码如下 复制代码

WHERE tt.term_id in (”.$cat_ID.”)

热门栏目