在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到。
代码一:
将下面代码加到主题模板适当位置,比如侧边栏:
- <?php
- = ;
- (is_single()){
- = get_the_category();
- = [0];
- = .->term_id;
- } (is_category()){
- ;
- = get_category();
- }
- (->category_parent != 0){
- = ->category_parent;
- = get_category();
- (->category_parent != 0){
- = ->category_parent;
- }{
- = ->term_id;
- }
- }{
- = ->term_id;
- }
- ?>
- <?php (!is_page()) { ?>
- <h3><?php ->cat_name; ?><span><?php ->slug; ?></span></h3>
- <ul id=>
- <?php wp_list_categories(.); ?>
- </ul>
- <?php } ?>
代码二:
将下面代码加到主题function.php模板文件中:
- get_category_root_id()
- {
- = get_category();
- (->category_parent)
- {
- = get_category(->category_parent);
- }
- ->term_id;
- }
调用显示代码加到主题模板的适当位置:
- <?php
- (is_category())
- {
- (get_category_children(get_category_root_id(the_category_ID(false)))!= )
- {
- \'<ul>\’;
- wp_list_categories(.get_category_root_id(the_category_ID(false)). );
- \'</ul>\’;
- }
- }
- ?>
暂无评论内容