繁体   English   中英

在每个类别页面上显示子类别

[英]Display subcategories on each category page wordpress

我在wordpress中创建了以下类别结构:

* Category1
    * SubCategory1(of Category1)
    * SubCategory2(of Category1)
* Category2
    * SubCategory1(of Category2)
* Category3

我需要在每个类别上列出其子类别。(没有任何帖子)所以当我访问一个类别时->我应该只看到这些子类别。 如果Main类别没有任何子类别->我应该看到它的帖子。

我的问题1: wordpress上的类别页面文件名是什么? (我搜索了很多,但没有明确的答案)。

我的问题1: 如何仅显示每个主要类别(例如:Category1)的子项。

在category.php页面上添加

 <?php
    if ( is_category() ) {
      $current_cat = get_query_var('cat'); ?>
<ul> 
   <?php wp_list_categories('&title_li=&show_count=1&child_of='.$current_cat); ?>
</ul> 
   <?php  } ?>

http://codex.wordpress.org/Template_Tags/wp_list_categories

对于分类法,获取子类别列表taxonomy-producttax_category.php:

 <?php $args = array(
            'show_count'         => 1,
            'child_of'           => get_queried_object()->term_id,
            'taxonomy'           => 'private_category', //define your taxeonomy
            'title_li'           =>'',
        ); ?>
        <ul>
         <?php wp_list_categories( $args ); ?> 
        </ul>

您可以使用以下代码来实现:

<?php
  if (is_category()) {
    $this_category = get_category($cat);
  }
?>
<?php
  if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0"); else $this_category = wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
 if ($this_category) { ?>
   <ul>
     <?php echo $this_category; ?>
   </ul>
<?php } ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM