【问题标题】:Display subcategories on each category page wordpress在每个类别页面 wordpress 上显示子类别
【发布时间】:2013-11-16 12:35:47
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 请检查我的答案。

标签: php html wordpress content-management-system


【解决方案1】:

添加到 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>

【讨论】:

  • 我使用了一个名为 producttax 的自定义分类法,创建了 producttax.php 并添加了代码,但它不起作用。我不明白我做错了什么......
  • 我也修改了 is_category() is_tax(' producttax ') 的代码,但没有成功
  • 复制 category.php 并使用这个名称 taxonomy-producttax_category.php。
  • 那行得通。谢谢:)。我设法使用“taxonomy-producttax.php”
  • pastel 你能再帮我解决一个问题吗?它显示子类别,但也显示帖子。我只需要显示帖子的一个子类别页面。
【解决方案2】:

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

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

【讨论】:

  • 我使用了一个名为 producttax 的自定义分类法,创建 producttax.php 并添加了代码,但它不起作用。
  • 我也修改了 is_category() is_tax(' producttax ') 的代码,但没有成功
猜你喜欢
  • 1970-01-01
  • 2014-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-07
  • 2014-04-09
  • 1970-01-01
  • 2012-03-08
相关资源
最近更新 更多