调用WordPress父子分类目录

在当前分类或者正文页面想调用显示与当前分类存在父子关系的分类目录时会用到。

代码一:

将下面代码加到主题模板适当位置,比如侧边栏:

  1. <?php
  2.      = ;
  3.     (is_single()){
  4.          = get_the_category();
  5.          = [0];
  6.          = .->term_id;
  7.     } (is_category()){
  8.          ;
  9.          = get_category();
  10.     }
  11.     (->category_parent != 0){
  12.          = ->category_parent;
  13.          = get_category();
  14.         (->category_parent != 0){
  15.              = ->category_parent;
  16.         }{
  17.              = ->term_id;
  18.         }
  19.     }{
  20.          = ->term_id;
  21.     }
  22. ?>
  23. <?php (!is_page()) { ?>
  24.     <h3><?php  ->cat_name; ?><span><?php  ->slug; ?></span></h3>
  25.     <ul id=>
  26.         <?php wp_list_categories(.); ?>   
  27.     </ul>
  28. <?php } ?>

代码二:

将下面代码加到主题function.php模板文件中:

  1.  get_category_root_id()
  2. {
  3.      = get_category(); 
  4.     (->category_parent) 
  5.     {
  6.          = get_category(->category_parent); 
  7.     }
  8.      ->term_id; 
  9. }

调用显示代码加到主题模板的适当位置:

  1. <?php
  2.     (is_category())
  3.     {
  4.         (get_category_children(get_category_root_id(the_category_ID(false)))!=  )
  5.         {
  6.              \'<ul>\’;
  7.              wp_list_categories(.get_category_root_id(the_category_ID(false)). );
  8.              \'</ul>\’;
  9.         }
  10.     }
  11. ?>
    © 版权声明
    THE END
    喜欢就支持一下吧
    点赞0 分享
    评论 抢沙发

    请登录后发表评论

      暂无评论内容