简体   繁体   中英

How can I show a list of parent categories and their sub category posts

I need to show lists in this order.. parent-sub-category-title => sub-category-title => sub-category posts. The following code gets me the parent title but doesn't give me the posts. Can anyone tell me why?

parent category title

sub-category title

sub-category-post

    //get all categories then display all posts in each term
    $taxonomy = 'commongood-categories'; //change this name if you have taxonomy
    $param_type = 'category__in';
    $term_args=array(
    'orderby' => 'name',
    'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {

        foreach($terms as $term){ //this foreach is for top level
        if($term->parent == 0){
        echo '<h2>'.$term->name.' </h2>'; //get top level category name
        $term_args=array(
        'orderby' => 'name',
        'order' => 'ASC',
        'child_of' => $term->term_id

         );

        $termss = get_terms($taxonomy,$term_args);
        foreach( $termss as $terms ) { 
        $args=array(
            "$param_type" => array($terms->term_id),
            'post_type' => 'commongood',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'caller_get_posts'=> 1

            );


            $my_query = new WP_Query($args);

            <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
                <li class="series-bubble">
                <div class="stext">
                    <span class="stitle"><a href="<?php echo get_permalink(); ?>"><?php if(get_field('optional_title')) { echo get_field('optional_title'); } else echo get_the_title(); ?></a></span>
                    <span class="scontent"><a href="<?php echo get_permalink(); ?>"><?php echo get_the_excerpt(); ?></a></span>
                </div>  
                </li>
            <?php endwhile;

           }
         }
       }
     }

You'll have to use a tax_query arg for custom taxonomies, category__in only works for categories: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Also, caller_get_posts has been deprecated for a while, use ignore_sticky_posts instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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