繁体   English   中英

在 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>

但是,我还想在列表中包含一个“显示全部”链接,该链接显示当前类别中的所有帖子(并且在选择时还采用活动的 state)。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM