如何调用WordPress父子分类目录
相信有很多使用 wordpress 模板的博友,把分类目录列表显示在博客首页,有利于提高用户体验。WordPress 经常用到的博客分类目录调用函数是 wp_list_cats,对于该函数,普通博主可能只会简单地应用,其实通过给它设置不同的参数,可以显示不同的分类目录列表的效果。在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到。
方法一:
将下面代码加到主题模板适当位置,比如侧边栏:
// CSS样式可以自己修改
<style>.crumbs_wrap #cat_list{border-top: 1px #eee solid;margin: 20px -20px 0;padding: 20px 0 0;}.crumbs_wrap ul{text-align: left;}</style>
<?php
$current = "";
if(is_single()){
$parent = get_the_category();
$parent = $parent[0];
$current = "¤t_category=".$parent->term_id;
}else if(is_category()){
global $cat;
$parent = get_category($cat);
}
if($parent->category_parent != 0){
$cat_id = $parent->category_parent;
$parent = get_category($cat_id);
if($parent->category_parent != 0){
$cat_id = $parent->category_parent;
}else{
$cat_id = $parent->term_id;
}
}else{
$cat_id = $parent->term_id;
}
?>
<?php if(!is_page()) { ?>
<h3><?php echo $parent->cat_name; ?><span><?php echo $parent->slug; ?></span></h3>
<ul id="cat_list">
<?php wp_list_categories("title_li=&child_of=$cat_id".$current); ?>
</ul>
<?php } ?>
方法二:
将下面代码加到主题 function.php 模板文件中:
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号
}
调用显示代码加到主题模板的适当位置:
<?php
if(is_category())
{
if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" )
{
echo '<ul>';
echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
echo '</ul>';
}
}
?>
http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除
资源客 » 如何调用WordPress父子分类目录
资源客 » 如何调用WordPress父子分类目录