输出wordpress列表类别,按名称计算父类别

时间:2017-07-28 09:14:43

标签: php mysql wordpress

我尝试通过在某处的代码中输入父类别来输出每个类别名称末尾带有count的wordpress类别。

下面的代码结果显示了最后的帖子数量,但它显示了所有类别。

例如:我有一个父类别名称“alpha”,其子类别名称是,类别A,类别B,类别C,类别D

我想要输出显示:

- 类别A(5)

- 类别B(2)

- 类别C(6)

- 类别D(7)

<?php
    $variable = wp_list_categories( array(
    'show_count' => true,
    'orderby'    => 'name',
    'style'      => 'none'
    ) );
    echo $variable; 
?>

2 个答案:

答案 0 :(得分:0)

我找到了答案,我只是使用&#34; child_of&#34;在数组中,并在值中输入父类别ID。

<?php
   $variable = wp_list_categories( array(
     'show_count'         => true,
     'orderby'             => 'name',
     'style'               => 'none',
     'hide_empty'         => 0,
     'child_of'            => 52
   ) );
   echo $variable; 
?>

答案 1 :(得分:0)

显示当前类别明智的子类别,总帖子数

<?php
    $category_object           = get_queried_object();
    $current_category_taxonomy  = $category_object->taxonomy;
    $current_category_term_id  = $category_object->term_id;
     $current_category_name  = $category_object->name;

    $args = array(
        'child_of'            => $current_category_term_id,
        'current_category'    => $current_category_term_id,
        'depth'               => 0,
        'echo'                => 1,
        'exclude'             => '',
        'exclude_tree'        => '',
        'feed'                => '',
        'feed_image'          => '',
        'feed_type'           => '',
        'hide_empty'          => 0,
        'hide_title_if_empty' => false,
        'hierarchical'        => true,
        'order'               => 'ASC',
        'orderby'             => 'name',
        'separator'           => '',
        'show_count'          => 1,
        'show_option_all'     => '',
        'show_option_none'    => __( 'No categories' ),
        'style'               => 'list',
        'taxonomy'            => 'category',
        'title_li'            => __( $current_category_name ),
        'use_desc_for_title'  => 0,
    );
    wp_list_categories($args);
 ?>