【问题标题】:wordpress show sub categories in parent categorywordpress 在父类别中显示子类别
【发布时间】:2015-02-07 13:58:46
【问题描述】:

我可以看到已经有很多关于这方面的信息,但我似乎找不到任何最新的信息,只是想知道是否有人可以帮助我。

我有不同的父类和子类,例如:

虚拟主机

  • 评论
  • 优惠券

域名注册商

  • 顶级注册商
  • 折扣代码

我在 category.php 页面中使用以下代码,它可以很好地显示每个类别中的子类别:

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

但是当我点击子类别链接时,它会显示该子类别中的所有帖子,但显然没有返回父目录等的链接。

有没有办法做到这一点?有没有人给我一些没有任何错误的代码?非常感谢。

【问题讨论】:

    标签: wordpress categories


    【解决方案1】:

    你可以将你的代码修改成这样:

    <?php
    
    if ( is_category() ) :
        $category = get_category( $cat );
        if ( $category->category_parent ) : // if category has parent
            $category_parent_id = $category->category_parent;
            $category_parent_link = get_category_link( $category_parent_id );
            echo '<a href="' . $category_parent_link . '">' . get_category( $category_parent_id )->name . '</a>';
        else : // else category has children
            $children = wp_list_categories( array(
                'child_of' => $category->cat_ID,
                'depth'   => 5,
                'echo'     => 0,
                'orderby' => 'id',
                'title_li' => '',
            ) );
            echo '<ul>' . $children . '</ul>';
        endif;
    endif;
    

    这是一种方法。还有其他方法。我可以为此建议这些功能:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      • 2014-04-09
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多