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 Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

Error Loading Portfolio Parts

My page seems to be stuck in a loop and I receive the following error message when trying to load:
Notice: Undefined property : WP_Query :: $the_post in ../query.php line 3960.

Here's my code from portfolio-page.php:

<?php
/* Template Name: Portfolio Page */
?>

<?php get_header(); ?>
<!-- "inserts" header.php --> 

<section class="row">
      <div class="small-12 columns text-center">
        <div class="leader">

        <!-- start the loop -->
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

            <h1><?php the_title(); ?></h1>

            <?php the_content(); ?>

        <!-- end while, start else -->
        <?php endwhile; endif; ?>

        </div>
      </div>
</section>

<?php
    $args = array(
                'post_type' => 'portfolio'
    );
    $query = new WP_Query( $args );
?>

<section class="row no-max pad">


    <!-- start the loop -->
    <?php if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post; ?>

<div class="small-6 medium-4 large-3 coulmns grid-item">
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>
</div> 

    <!-- close the loop -->
    <?php endwhile; endif; wp_reset_postdata; ?>


</section>

<?php get_footer(); ?>
<!-- "inserts" footer.php -->

?>

I've just formatted the code in the question so it was a little easier to read. The back ticks were there but it just needed an extra line break before and after.

Thanks -Rich

Hi Rich. Glad you did that, it's been driving me nuts all week and I've been meaning to ask one of the mods about it!
I just checked the edited code, and not sure what you did?
You mentioned needing a line-break before and after....do you mean before the beginning ``` and after them? Is a line-break defined by an enter or 2 spaces? I ask b/c I'm pretty sure I did that but I guess I didn't!

Hi,

No problem, you pretty much had everything set up fine. It just needed an enter, so it displays as:

<?php echo 'This is code'; ?>

and not:

<?php echo 'This is code'; ?>

The first example has text, empty line, backticks start whereas the example below just has text and backticks. You can see the first example is correct.

Hope that helps.

Also, I've answered your question below :)

-Rich

1 Answer

Hi,

Can you try changing: wp_reset_postdata to wp_reset_postdata() on this line:

<?php endwhile; endif; wp_reset_postdata; ?>

Does that make any difference?

EDIT:

You'll also need to change $query->the_post to $query->the_post() on this line:

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

-Rich

HI again Rich.
wp_reset_postdata() fixed an error that was not showing up earlier due to being stuck in the loop! the_post() did the trick to get the loop working correctly! Thanks!!
Oh, and thanks for the markup clarification.

Another question for you Rich. Now the Portfolio parts are rendering, however they are stacking vertically and not in accordance w/ the foundation grid...I'm trying to track down the problem but can't seem to find it.

No problem :)

Regarding the stacking of portfolio parts, I'd be guessing a little but it could be to do with the typo of columns here:

<div class="small-6 medium-4 large-3 coulmns grid-item">

If that isn't the issue I'd recommend using the browser developer tools (usually F12 or right click and 'Inspect Element') to identify the issue.

Hope that helps :)

-Rich

yeh surprisingly columns was the issue....dyslexia and coding aren't the best combo!! ;-)

Thanks for all the help Rich!

No problem, glad I could help :)