wordpress loop the content of a wp query in a common div

this code should work

<div class="js-filter">
    <?php
        $get_parent_cats = array(
        'taxonomy' => 'category',
        'hierarchical' => true,
        'orderby' => 'term_order',
        'parent' => '0' //get top level categories only
    );

        $all_categories = get_categories( $get_parent_cats );//get parent categories

        foreach( $all_categories as $single_category ){
            //for each category, get the ID
            $catID = $single_category->cat_ID;

            //category name & link
            echo '<h3>' . $single_category->name . '</h3>';
            
            
            $query = new WP_Query( array(
                'post_type' => 'jobs',
            
                'showposts' => -1,
                'category__in' => array($single_category->term_id),
                'category__not_in' => 6 ,
            ) );
            
            ?>
            <div class="job_position">
                <ul>
                <?php 
                    // Posts for the parent category (should be none)
                    while( $query->have_posts() ):$query->the_post();
                ?>
                <li><?php the_title(); ?></li>
                <?php
                endwhile;
                wp_reset_postdata();
                ?>
                </ul>
            </div>
            <div class="job_location">
                <ul>
                    <?php 
                        // Posts for the parent category (should be none)
                        while( $query->have_posts() ):$query->the_post();
                    ?>
                    <li><?php the_field('location') ; ?></li>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </ul>
            </div>
            <div class="read_more">
                <ul>
                    <?php 
                        // Posts for the parent category (should be none)
                        while( $query->have_posts() ):$query->the_post();
                    ?>
                    <li><a href="<?php the_permalink(); ?>">Read more</a> </li>
                    <?php
                    endwhile;
                    wp_reset_postdata();
                    ?>
                </ul>
            </div>
    <?php 
        }
    ?>
 </div>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top