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 Homepage Templates in WordPress A Static Homepage Template

Susie Broughton
Susie Broughton
2,130 Points

get_template_part() not working!

At 3.54 Zac checks to see his code is working - all my images on the portfolio and home vanish after using the code <?php get_template_part('content','portfolio'); ?> in the page-portfolio.php template. I tested with just a bit of text in the content-portfolio.php page and again it didn't fetch that.

Code for content-portfolio.php: <?php

$args = array(
 'post_type'=>'portfolioguff'

);
$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('medium'); ?></a>
</div>

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

</section>

Code for page-portfolio.php:

<?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>WHY NOTHING WORK!!!</p>
    <?php the_content(); ?>   

  <?php endwhile; endif; ?> 

</div>

</div> </section>

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

<?php get_footer(); ?>


When I remove the get_template_part from the above and paste the loop back in it's works fine again so it's definetly that causing the issue! Any ideas appreciated!

3 Answers

Ryan S
Ryan S
27,276 Points

Hi Susie,

I looked through your code and I can't see any glaring errors.

The one thing that is different is that in content-portfolio.php you have referenced your post_type as "portfolioguff', and Zac just called his "portfolio". But you did say that the code works if you paste it back into page-portfolio.php so I'm assuming you named your custom post types "portfolioguff". But in the event it is a typo, I thought it was worth bringing up.

Another thing to check is your directory structure. Are both files in the same directory?

Susie Broughton
Susie Broughton
2,130 Points

Thanks for the reply. Yes all in the same file structure. Because of earlier lessons it wasn't possible to use the name portfolio as it was already taken, so it's not a typo!

Susie Broughton
Susie Broughton
2,130 Points

I just tested with getting another file and it worked, so I pasted the content into the new file and it worked ok, for some reason it didn't like that file. I renamed the new file content-portfolio and it's now working fine. Must have been a bug with the file not a WP quirk!

Ryan S
Ryan S
27,276 Points

Glad you got it working!