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

PHP How to Build a WordPress Theme WordPress Theme Functions Displaying Custom Posts and Fields in a Template

Rick Banich
Rick Banich
6,846 Points

Displaying Custom Posts and Fields in a Template.

When I add the following code to my work.php file:

<?php

$args = array(
    'post_type' => 'work'
);

$the_query = new WP_Query( $args );

?>

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

Then refresh my work page, I still get the header and footer, but no longer get anything in between. "This is a work.php file." shows up but nothing below that until it gets to "This is a Footer."

1 Answer

Maja B.
Maja B.
12,984 Points

You open an if statement but do not finish it. You miss the get_template_part( 'content', 'work' ) + the endwhile and endiif statement. See below ...

    <?php

        $args = array(
            'post_type' => 'work'
        );

        $the_query = new WP_Query( $args );

    ?>

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

        <?php get_template_part( 'content', 'work' ); ?>

    <?php endwhile; endif; ?>

You need to add $the_query before if have_posts() as well

If ( $the_query->have_posts() )