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

Displaying Custom Posts and Fields in a Template

It seems that when adding in the custom work posts, only the most recent post will show. Here is my work.php. Any help is appreciated. Thanks!

<?php
/*

Template Name: Work Page

*/

get_header();   
?>

<p>This is the work.php</p>

<?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(); ?>

<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<?php the_field( 'description' ); ?>
<hr>

<?php endwhile; else: ?>

<p>404 Message</p>

<?php endif; ?>

<?php get_footer(); ?>

8 Answers

I noticed that it wasn't displaying the "There are no pages or posts" piece in the loop so I started from scratch... It WORKED!!! Thanks for all the help! So stoked for the next module on WP!

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Do you mean the most recent blog posts or recent projects?

The recent projects.. I have three projects added in the admin console and only the most recent will display. Thanks!

Hi, fellas. I'm stuck at this same part, and I think I may have missed a part of the project.

Can anyone point me to the video where we added projects? All I can find in the history is adding in posts in the category 'Work.'

Did a little more digging and discovered what I was missing. Here's how I fixed my issue, hopefully this will help someone else. If you're missing the "Dr. Wattz," "Shirts 4 Mike," & "Stop Visual Pollution" content, this is the solution.

I received the badge for the first section of videos,"Content Strategy with WordPress", but somehow, I never saw the final video in which we install a couple plugins and setup the custom post type entitled "Adding Custom Content to the Site".

At the end download the Project Files and add the content. There's a ,txt file ("work-projects.txt") with the relevant details for each project.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

So you added three in the backend. Check. And how many are showing up on the front-end? You can use the posts_per_page parameter to hard code 'x' number of posts to test it.

Okay so I added in the 'posts_per_page' parameter and it still only shows just one. Here's the code I added.

<?php

$args = array(

'posts_per_page=' => 3,
'post_type'       => 'work'

);

$the_query = new WP_Query( $args );

?>
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

What does the code for your Loop opening look like?

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

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hmm, if you mark that most recent project to draft what shows up?

The next project in line.

Gautam Thapar
Gautam Thapar
5,265 Points

May be try it without the if condition.