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



我的索引中有以下代码片段.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_option_all",但这只会将我带到所有帖子,而不是父类别中的所有帖子。我也尝试将深度更改为 0,但这没有效果。我一直在寻找解决方案,但找不到与此确切问题相关的任何内容。

可能不是理想的解决方案,但我最终所做的是在变量中添加显示所有项目,然后使用 str replace 检查该类并将其添加到"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 } ?>

最新更新