Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress WordPress Custom Loops with WP_Query

show 2 or more category

hai there...

this tutorial about wp_query are great, but i'm curious, what if i wanna show 2 category or more in one page, like newspaper page, is that possible ?

1 Answer

Stephen O'Connor
Stephen O'Connor
22,291 Points

You can just set what categories to show in the arguments of your WP_Quuery.

You can either choose to show more than one category in the one WP_Query loop or you can have multiple loops to show different categories on different sections of the page. If you have more than one loop on the page make sure you reset your query.

Code is only an example.

<?php 

    $args = array(
        'post_type' => 'post',
        'cat' => 33,
        'posts_per_page' => 1
    );

    $the_query = new WP_Query( $args );

?>

<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

    <?php
        $youtube = get_field('youtube_embed_code');
    ?>

    <?php echo "$youtube"; ?>

<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endif; ?>