Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress WordPress Theme Development Custom Post Type Templates in WordPress The Portfolio Homepage

Avery Kushner
Avery Kushner
854 Points

Notice: Undefined variable: query

Im getting this error when on my portfolio page.

Here is my code:

<?php /*

Template Name: Portfolio Page

*/ ?>

<?php get_header(); ?>

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

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

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

<?php endwhile; else: ?>

<?php endif; ?>

    </div>
  </div>

</section>

<!-- working with wp_query -->

<?php

$args = array('post_type' => 'portfolio_post');
$the_query = new WP_Query( $args );

?>

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

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

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

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

</section>

<?php get_footer(); ?>

1 Answer

Avery Kushner
Avery Kushner
854 Points

Update: I've isolated the error, it is definitely the $query->the_post(); throwing the error.

If I replace that with an echo '<div>I worked!!</div>' it outputs, in an infinite loop: I worked!!

Any ideas on why is it infinite looping? I certainly only have one post of type portfolio_post.

Yes, my post_type's slug is portfolio_post.

Is there a way to definitively check what post_type a post is. After creating a custom post type, a tab popped up titled Portfolio as per the video, which is my only indication that any posts added through that tab are of type portfolio_post, but I have my suspicions given my error.

Thanks,

Avery Kushner
Avery Kushner
854 Points

Problem solved I had the_post(); instead of $the_query->the_post();