wp_list_categories取决于所选标签

时间:2015-10-30 09:59:12

标签: wordpress woocommerce wp-list-categories

我想根据所选标签显示每个类别中当前帖子数量的类别列表。

例如,如果选择了标签A(标记为2个帖子),则类别列表将为:

- cat A (2)
-- cat Aa (1)
-- cat Ab (1)

如果选择了标签B(标有3个帖子):

- cat A (3)
-- cat Aa (1)
-- cat Ab (1)
-- cat Ac (1)

由于无法在wp_list_categories的参数中指定标记,您是否有关于如何处理的想法?

由于

3 个答案:

答案 0 :(得分:1)

使用wp_list_categories时,无法与post标签结合使用。因此,首先根据标记ID筛选帖子,然后根据这些帖子ID,找到如下代码所示的类别:

<h2><?php _e('Categories by Tags'); ?></h2>
<form action="" method="post">
      <?php wp_dropdown_categories('taxonomy=post_tag&show_option_none=Select tag&orderby=name&name=posttag&selected='.$_POST['posttag']); ?>
      <input name="btnSubmit" id="btnSubmit" type="submit" value="View" />
</form>
<?php 
if(isset($_POST['btnSubmit']))
{
    $the_query = new WP_Query( array( 'tag_id' => $_POST['posttag'] ) );
    $categories = array();

     // The Loop
     if ( $the_query->have_posts() ) {

        while ( $the_query->have_posts() ) {
                $the_query->the_post();
                $post_cat = get_the_terms($post->ID, 'category');

                foreach ($post_cat as $key => $value)
                {
                    if($value->parent==0)
                    {
                        $categories[$value->term_id]['main_category']=$value;
                    }
                    else
                    {
                        $categories[$value->parent]['sub_categories'][$value->term_id]=$value;
                    }

                }
        }

        echo "<ul>";
        foreach ($categories as $cat_key => $cat_value)
        {
             foreach ($cat_value as $c_key => $c_value)
             {
                    if($c_key == "main_category")
                    {
                        echo "<li>".$c_value->name." (".$c_value->count.")";
                    }
                    if($c_key == "sub_categories")
                    {
                        echo "<ul>";
                        foreach ($c_value as $sc_key => $sc_value)
                        {
                            echo "<li>".$sc_value->name." (".$sc_value->count.")</li>";
                        }
                        echo "</ul></li>";
                    }

             }
        }
        echo "</ul>";
    } else {
         // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}
?>

答案 1 :(得分:0)

你有一堆可以应用wp_list_categories函数的参数。其中一个是show count,它是一个布尔值0 - &gt; false,1 - &gt;真正。检查child_of参数。

$args = array(
    'show_option_all'    => '',
    'orderby'            => 'name',
    'order'              => 'ASC',
    'style'              => 'list',
    'show_count'         => 0,
    'hide_empty'         => 1,
    'use_desc_for_title' => 1,
    'child_of'           => 0,
    'feed'               => '',
    'feed_type'          => '',
    'feed_image'         => '',
    'exclude'            => '',
    'exclude_tree'       => '',
    'include'            => '',
    'hierarchical'       => 1,
    'title_li'           => __( 'Categories' ),
    'show_option_none'   => __( '' ),
    'number'             => null,
    'echo'               => 1,
    'depth'              => 0,
    'current_category'   => 0,
    'pad_counts'         => 0,
    'taxonomy'           => 'category',
    'walker'             => null
);
wp_list_categories( $args ); 

https://codex.wordpress.org/Template_Tags/wp_list_categories

答案 2 :(得分:0)

这应该有效

GET_DATE