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

时间:2013-11-15 13:06:05

标签: php html html5 wordpress content-management-system

我在wordpress中创建了这个类别结构:

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

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

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

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

2 个答案:

答案 0 :(得分:2)

  

在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

  

for taxonomy获取子类别列表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>

答案 1 :(得分:0)

您可以使用此代码实现此目的:

<?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 } ?>