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.

PHP How to Build a WordPress Theme WordPress Theme Functions Displaying Custom Posts and Fields in a Template

Julian Ptak
Julian Ptak
30,920 Points

Wordpress work.php not showing descriptions

I'm not sure what I'm doing wrong here. My work.php file isn't showing the descriptions for the various projects. Does anyone have any suggestions for how to debug this? I've checked my code over but I'll post it below anyway:

<?php get_header(); 

/*

    Template Name: Work Page

*/


?>

<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><?php the_title(); ?></h3>
    <?php the_field( 'description' ); ?>
    <hr>

<?php endwhile; else: ?>

    <p>There are no posts or pages</p>


<?php endif; ?>

<?php get_footer(); ?>

My second guess is it is a problem with some setting in the admin section but I'm at a complete loss as to how to narrow down which one it could be. How does one debug Wordpress? It seems a bit difficult. Any ideas? Thanks!

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

When ever you use WP_Query you need to add the query object to the "if", "while", and "the_post". So your if statement should look like this:

<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
    <h3><?php the_title(); ?></h3>
    <?php the_field( 'description' ); ?>
    <hr>

<?php endwhile; else: ?>

    <p>There are no posts or pages</p>


<?php endif; ?>

Right now it isn't printing anything because the if statement is failing, because the main page query the runs for every wordpress page doesn't have any posts. You need to tell the if state to check your custom query instead of the standard one.

Julian Ptak
Julian Ptak
30,920 Points

Thank you for your post! I added the change but the descriptions still aren't showing.

Andrew Shook
Andrew Shook
31,709 Points

Is it showing the title?

Julian Ptak
Julian Ptak
30,920 Points

Yes the title is showing just not the descriptions.

Andrew Shook
Andrew Shook
31,709 Points

Ok, I know this is a long shot, but make sure the spelling of "description" is the same as the field name you gave the field. I've had this happen in the past.

EDIT: Actually the problem is that you probably didn't add "?>" after "the_post();"

Julian Ptak
Julian Ptak
30,920 Points

Stupid question but... on the "Work" page when you go to add the description to it, should that box be marked with a title like "Description"? Because it's just an open text box right now and I'm thinking there is some kind of disconnect between the custom post type and the custom fields. The teacher goes through it all so quickly in the videos I think I might have missed something.

Andrew Shook
Andrew Shook
31,709 Points

Actually the problem is that you probably didn't add "?>" after "the_post();". This is how you have it:

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

This is how it should be:

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

I'm not sure what you mean when you say "that box". What are you referring to?

Julian Ptak
Julian Ptak
30,920 Points

It's the same spelling. :-/ And it's there. You need to scroll right to see it. I'll take away the whitespace in my question.

Julian Ptak
Julian Ptak
30,920 Points

In the admin section of the site, you need to go and add custom fields for the post types like Description and URL and such. Well I added a initial line of text to appear in the textbox when I create a new project and add a "description". But when I add a new "work" project, the initial text isn't there. This is a longwinded way of saying for some reason I don't think that the "Work" custom post type and the "Description" field are... linked? I'm sorry I can't phrase this better. This is my first time really doing any heavy lifting with Wordpress and I'm kind of confused.

Andrew Shook
Andrew Shook
31,709 Points

Yes you have to add it manually. Go back into your custom field, description i believe, and scroll down to the section call "Location". I that section you will see some select list that should say something along the line of "Post type" "is equal to" and the last one should have a post type. Click the button below that says "add rule group". Then set the post type equal to work.

Julian Ptak
Julian Ptak
30,920 Points

That did it! I added your initial answer as the best answer so you'd get the points on your profile! Thanks so much! You da man!

Andrew Shook
Andrew Shook
31,709 Points

So which was it? Adding the closing php tag or was the field not added to the post type?

Julian Ptak
Julian Ptak
30,920 Points

Field was not added to the post type. Noob mistake, I'm sure. :)

Andrew Shook
Andrew Shook
31,709 Points

Nope, I've built 25 websites( 10 or so are WP) and I still make mistakes like that. Difference is I know where to go to trouble shot the problem.

Julian Ptak
Julian Ptak
30,920 Points

I appreciate you saying that. Sometimes it's all a bit overwhelming. I'm sure it gets easier with time.

Andrew Shook
Andrew Shook
31,709 Points

It'll never be a matter of getting it perfect every time. I would say that the most important skill in any programming (web, app, or computer) is learning to trouble shot a problem. Everyone makes mistakes, but figuring out how to fix them in a timely manner and asking for help is what sets people apart.

At the last company i worked for, the Sr. front-end developer was working on a piece of Javascript that had a bug. There were 3 separate timers on a single page, but only one of them was working. He debugged that code for 2 hours before he asked me to take a look at it. I found the problem in about 2 minutes, because the code was fresh to me.