AP_19
AP_19

Reputation: 41

Wordpress output Category Children Posts of single Parent Category

Struggling a bit with what I have currently. So I want to loop through a single parent category and output all child category posts with a link.

Current code I have is below, that creates a list for each parent and child category, so this:

I feel it's my $args that might be able to fix this.

<?php  

    $get_queried_object = get_queried_object();

    $cats = get_categories( array( 
        'child_of'   => $get_queried_object->term_id,
        'hide_empty' => false
    ) ); 

    foreach ($cats as $cat) :

        $args = array(
                    'category_name' => 'services', // Use the category id, can also replace with category_name which uses category slug
          'cat' => $cat->term_id
        );

        $my_query = new WP_Query($args); ?>

            

        <?php 
        if ($my_query->have_posts()) :
        echo '<h5>'.$cat->name.'</h5>'; ?>


            <ul>
                <?php while ($my_query->have_posts()) : $my_query->the_post();  ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        <?php else : 

            echo 'No Posts for '.$cat->name;                

        endif; 

    endforeach; 
?>

So that shows the below, and I've put to the right what we want and also shown the Category setup.

what is required and what we have

Upvotes: 1

Views: 231

Answers (1)

Bhautik
Bhautik

Reputation: 11282

Use cat instead of category__in and also you need to run wp_reset_postdata() afterwards use the main query’s current post again. try the below code.

<?php  

    $get_queried_object = get_queried_object();

    $cats = get_categories( array( 
        'child_of'   => $get_queried_object->term_id,
        'hide_empty' => false
    ) ); 

    foreach ($cats as $cat) :

        $args = array(
          'cat' => $cat->term_id
        );

        $my_query = new WP_Query($args); 

        if ($my_query->have_posts()) : ?>
            <ul>
                <?php while ($my_query->have_posts()) : $my_query->the_post();  ?>
                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                <?php endwhile; wp_reset_postdata(); ?>
            </ul>
        <?php else : 

            echo 'No Posts for '.$cat->name;                

        endif; 

    endforeach; 
?>

Updated as per OP code.

<?php
/**
 * Template Name: Home
 * Description: Page template with custom content
 *
 */

get_header();

?>

<section>
    <div class="container">
        <div class="row">
            <div class="col">
                <?php  
    
                    $cats = get_categories( array( 
                        'child_of'   => 2,
                        'hide_empty' => false
                    ) ); 

                    foreach ($cats as $cat) :

                        $args = array(
                          'category_name' => 'services', // Use the category id, can also replace with category_name which uses category slug
                          'cat' => $cat->term_id
                        );

                        $my_query = new WP_Query($args); ?>

                        <?php 
                        if ($my_query->have_posts()) :
                            echo '<h5>'.$cat->name.'</h5>'; ?>
                            <ul>
                                <?php while ($my_query->have_posts()) : $my_query->the_post();  ?>
                                    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
                                <?php endwhile; wp_reset_postdata(); ?>
                            </ul>
                        <?php else : 

                            echo 'No Posts for '.$cat->name."</br>";

                        endif; 

                    endforeach; 
                ?>
            </div>
        </div>
    </div>
</section>

<?php
get_footer();
?>

Upvotes: 2

Related Questions