【问题标题】:How to display child-category of a parent category如何显示父类别的子类别
【发布时间】:2018-05-01 11:57:14
【问题描述】:

如何获取页面中当前类别的子类别?我们的代码在这里

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$category = get_post_meta( get_the_id(), 'category', true );

print_r( $category); 

【问题讨论】:

标签: wordpress


【解决方案1】:

您必须从当前查询中获取一个类别并将一个子句“child_of”放入参数中。请检查以下代码:

$current_cat = get_queried_object();

$args = array( 'child_of' => $current_cat->term_id, );
$categories = get_categories( $args );
//print_r($categories );
foreach($categories as $category) { 
    echo $category->name;
}

【讨论】:

  • 它显示所有类别和子类别
  • 您需要根据自己的需求修改代码
【解决方案2】:

这段代码对我有用。

<?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    ?>
    <?php
    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>

<?php } ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 2020-01-10
    • 2014-04-09
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多