在 WordPress 的当前类别页面上显示所有子类别和父类别

Show all sub categories and parent on current category page in WordPress

我的 index.php 中有这段代码片段,用于显示当前类别页面的所有子类别:

<?php
if (is_category()) {$this_category = get_category($cat); }

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&depth=1&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>

但是我还想在列表中包含一个 "Show All" link,它显示当前类别中的所有帖子(并且在选择时也采用活动状态)。

我已经在 args 中尝试 "show_option_all" 但这只会将我带到所有帖子而不是父类别中的所有帖子。我也曾尝试将深度更改为 0,但这没有任何效果。我一直在寻找解决方案,但找不到与这个确切问题相关的任何信息。

可能不是理想的解决方案,但我最终做的是在变量中添加显示所有项目,然后使用 str replace 检查 class 并将其添加到 'all' 项目如果没有设置。

<?php
if (is_category()) {$this_category = get_category($cat); }

if($this_category->category_parent)
    $this_category = '<li class="cat-item-all"><a href="/category/'. $this_category->category_parent .'/">All</a></li>'.wp_list_categories('orderby=id&hide_empty=1&title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent."&echo=0");
else
    $this_category = '<li class="cat-item-all"><a href="#">All</a></li>'.wp_list_categories('orderby=id&hide_empty=1depth=0&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");

if ($this_category) { 

    if(strpos($this_category,'current-cat') == false) { 

$this_category = str_replace('cat-item-all', 'cat-item-all current-cat', $this_category); }?>
    <?php $all_posts_url = get_post_type_archive_link( 'category_parent' ); ?>
    <ul>
        <?php echo $this_category; ?>
    </ul>
<?php } ?>