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

Larry Wright
Larry Wright
7,325 Points

Displayiin Custom Posts and Fields in a Template Problem

I am having trouble getting my code to work in my work.php file. The section I noticed the problems starting was Websites How to Build a WordPress Theme WordPress Theme Functions Displaying Custom Posts and Fields in a Template. At about 4:09 of the video I noticed that the Title of the posts was not showing and neither did the descriptions for each post at 5:03 of the video. At this time I noticed that the word, Work did not appear in the Allisongrayce.com/work as it should. Below is the code that displayed:

This is the header. Home About Blog Contact Work This is the work.php file This is the footer.

And this is the code that should have displayed: This is the header. Home About Blog Contact Work This is the work.php file Work (there should also be a line below this, between Work and the footer This is the footer.

I think the code has already been missed. I have looked at other questions on this section in the forum and I have put my plug-ins of Custom Fields and Custom Post Types. I have all of my downloaded folder and files in the Allisongrayce.com folder. I went over all the videos in this section of Wordpress Theme Functions and still can’t figure out my mistake. I think it begins where the WORK and the line below it is not displaying but not sure what to do. Thanks for your help. I am going to paste the code through the end of the above mentioned video from the work.php file, remember this is through the end of that video

<?php 
/*
    Template Name: Work Page
*/
get_header(); ?>
<p>This is the work.php file</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>There are no posts or pages here</p>
<?php endif; ?>
<?php get_footer(); ?>

Thanks for your time, Larry

10 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Okay, quick first check here:

  1. What did you name your Work custom post type?
  2. Have you entered in custom work entries in the admin area?
Larry Wright
Larry Wright
7,325 Points

I named the Work custom post type, work. I have entered all of the custom work entries into the admin area. Thanks for your help, Larry

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

What happens when you copy and paste in the code from the project downloads? (In the first video of the stage)

Larry Wright
Larry Wright
7,325 Points

I have copied the wp-theme-development-stage-02 and put it in the allisongrayce.com file, though I am not sure that I should have. The only work.php file I found in the wp-o2 folder mentioned earlier, then wp-content>themes>wp-portfolio>had a work.php file that was empty. I have the html-prototype file in the allisongrayce.com file as well and it has the work.php file code already posted in this forum question I read that if someone wanted to download the code for the site to use the username and password of treehouse but I have no idea where to go to do that. I believe that a lot of your vidfeos assume that everyone knows when and where to place the files and that is not the case with me at least. I have about 7 websites that I have on a host server to bring to life and I wanted to learn Wordpress to bring them to life right now. I have spun my wheels for three years trying to learn about web development and like what I'm learning with Treehouse, but the more I learn the more stupid I feel. I will not go further in Wordpress until I get this straightened out, but I will work on a lot of the deep dives in CSS and PHP until you help me figure this out. Thanks for helping rookies. Larry

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hi!

You should only need to copy the single work.php file code. The file is located in wp-content/themes/wpportfolio/work.php.

If the file is blank, which is strange, try looking at the code in the next download and try comparing that. From what I can see the code you pasted is correct.

However you mention your navigation isn't appearing either. Did you use the navigation code to add the navigation in the header.php?

Larry Wright
Larry Wright
7,325 Points

Hi I went through all the videos on the first three sections of WordPress Themes of Content Strategy with WordPress, WordPress Theme Templates and WordPress Theme Functions. Everything worked fine until the last video of WordPress Theme Functions with Displaying Custom Posts and Fields. I took out the old code which you said looked correct and what I recoded quit working around the 4 minute mark when I had to paste $the_query into the code below

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

Work.php still worked after this wp_query code was added below:

<?php

$args = array(
    'post_type'=>'work'
);

$the_query = new WP_Query( $args );

?> When I went through it the second time I did notice that I had a title and <hr> tag working until the $the_query problem. I went over every video of the first three WordPress themes and I couldn’t find anywhere that I typed the titles of the work posts of Stop Visual Pollution, Dr. Waltz, Smells Like Bakin, and Shirts 4 Mike or the description. The path that you told me to get to the work.php file is the one I have set up in my projects file in sublime which has all the files you helped me build. I looked in original downloads and the only work file was work.html and not work.php. I am sorry I am so much trouble. I cannot find a work.php file that has anything other than what I created. What does the WP query do to the code exactly and where do the titles and descriptions come into play.
I had this as part of my code in the header.php file, wp_nav_menu ( $args ); which I think will answer your one question.
Thanks again, Larry

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hi Larry,

Does this code work for you:

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

        <h4>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </h4>

        <?php the_field( 'description' ); ?>

<?php endwhile; else: ?>

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

<?php endif; ?>
Larry Wright
Larry Wright
7,325 Points

Thanks again, the code didn't work though. I hate to keep wasting your time. I will do PHP for a while and start the whole three sections over to try to correct my problem. I still don't understand where $the_query is going to pull the information for the titles and descriptions into the work.php file. We have been responding back and forth for 8 days and I am still stuck and taking too much of your time. I will give you a break and not deal with WordPress for a while. Larry

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hey Larry,

Don't worry about it from my end! It's my job to help you work through this and I'm happy to do it :)

To explain a bit.. When we use the code

$the_query = new WP_Query( $args );

It is the WP_Query function that is getting all of the title, description, and everything else about the posts with the post type of 'work' from the database.

Also, you probably do, but you did set the Work page to use the work template, correct?

I'm happy to help troubleshooting this with you, but you're right that it's often a good idea to step away from a project for a bit and then come back to it with a clear head :)

I am having the same this exact same issue.

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Hi David,

Is the problem you're having with the navigation not appearing correctly or the items with the custom post type 'work'?

I figured out where the issue was.

Thanks