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 trialJulian Ptak
30,920 PointsWordpress 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
31,709 PointsWhen 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
30,920 PointsIn 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
31,709 PointsYes 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
30,920 PointsThat 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
31,709 PointsSo which was it? Adding the closing php tag or was the field not added to the post type?
Julian Ptak
30,920 PointsField was not added to the post type. Noob mistake, I'm sure. :)
Andrew Shook
31,709 PointsNope, 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
30,920 PointsI appreciate you saying that. Sometimes it's all a bit overwhelming. I'm sure it gets easier with time.
Andrew Shook
31,709 PointsIt'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.
Julian Ptak
30,920 PointsJulian Ptak
30,920 PointsThank you for your post! I added the change but the descriptions still aren't showing.
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsIs it showing the title?
Julian Ptak
30,920 PointsJulian Ptak
30,920 PointsYes the title is showing just not the descriptions.
Andrew Shook
31,709 PointsAndrew Shook
31,709 PointsOk, 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
30,920 PointsJulian Ptak
30,920 PointsStupid 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
31,709 PointsAndrew Shook
31,709 PointsActually 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
30,920 PointsJulian Ptak
30,920 PointsIt'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.