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

My Portfolio Pieces are not showing

Hey,

So my portfolio pieces are supposed to show up on the Portfolio Page in the Wordpress Theme Development Course.

I followed along with Zac and my code is the same, but the custom loop doesn't seem to be working on my page-portfolio.php template. I tried putting a test in to see if it works, and it did, so I'm kind of confused.

Here's the code for the page-portfolio.php template

<?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>

        <?php the_content(); ?> 

<?php endwhile; endif; ?>

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

<?php

    $args = array(

    'post_type'=>'portfolio'

    );

    $query = new WP_Query($args);

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

 <?php if( $query->have_posts() ) : while ( $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('large'); ?></a>

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

</section>

<?php get_footer(); ?>

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Robin, I don't see any syntax errors or missing spellings so my only suggestion would be to go back and make sure that you did in fact name the post type slug "portfolio" and that you set the page template to the correct template. I know it's unlikely, but just double check. After that I would change the post_type to post and see if it will pull in anything at all.

Andrew,

I put in a TEST to see if the Query loop works and it does. I have 4 images, and it loops through 4 times and puts TEST 4 times. This is what I did...

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

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

                <p>TEST</p>

                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('large'); ?></a>

           </div>

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

...so I know the loop is working, but for some reason the images are not showing.

Oh my God...I forgot to set the featured image on the portfolio post type...when I did, it worked. Thanks Andrew.

--and make sure that you did in fact name the post type slug "portfolio"

I'm not sure how to do that?

a bit late to be answering this but in case someone else is interested at some point:
you set the slug from within the Custom Post Type plugin we installed.