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

htmlol

我的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,但这没有效果。我一直在寻找解决方案,但找不到与此确切问题相关的任何内容。

htmlol

可能不是理想的解决方案,但是我最终要做的是在变量中添加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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

子类别在父页面上设置为当前类别

在子类别页面上显示wordpress子类别帖子

类别和子类别显示(父/子)

WordPress模板-显示按子类别分隔的父类别下的所有帖子

如何在点击类别页面上显示子类别WordPress类别

在所有类别和帖子模板上获取WordPress当前类别ID和顶级父类别ID

仅显示当前类别页面的子类别

WordPress类别菜单中的当前类别

PHP / Magento:根据类别页面上的子类别,按产品显示,排序和限制

Wordpress:我想在 wordpress 的页面上显示所有类别的最新产品?

如何在子类别页面中显示Wordpress类别?

仅在单击父类别时加载和显示子类别

Wordpress中父类别之前的子类别

将父类别和子类别与帖子分组,并在WordPress中使用Timber / Twig进行显示

SQL获取所有类别和所有子类别

显示产品和子类别的WooCommerce类别页面

在Wordpress的单个页面上显示某个类别的所有帖子

WooCommerce从当前类别页面获取父类别

来自父类别和所有子类别的SQL Server计数记录

类别和子类别的父 ID

如何显示类别页面的子类别?

如何显示类别,子类别和子子类别-Codeigniter

WordPress类别-列出帖子和子类别

按父项或 ID 在单个页面上显示 Woocommerce 子类别

在Laravel中显示类别和子类别

显示数组中的类别和子类别

以嵌套格式显示类别和子类别

WordPress针对特定页面上的特定帖子类别

在WooCommerce子类别存档页面上显示子类别术语列表