我的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>
但是,我也想在列表中包括一个“显示全部”链接,该链接显示当前类别的所有帖子(并且在选中时也采用活动状态)。
我已经在args中尝试了“ show_option_all”,但这只是将我带到所有帖子,而不是父类别中的所有帖子。我也尝试将深度更改为0,但这没有效果。我一直在寻找解决方案,但找不到与此确切问题相关的任何内容。
可能不是理想的解决方案,但是我最终要做的是在变量中添加show all项,然后使用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 } ?>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句