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

Tat Angwattanapanich
Tat Angwattanapanich
2,566 Points

Having trouble displaying the "Work" page in Wordpress

According to "Displaying Custom Posts and Fields in a Template" lesson, i followed the instructor everything. However, it failed to display the title and the description of the Work page. It displayed only get_header() and get_footer(). It's like WP_Query() didn't work.

the link is as follows.

http://teamtreehouse.com/library/how-to-build-a-wordpress-theme/wordpress-theme-functions/displaying-custom-posts-and-fields-in-a-template-2

this is my code

<?php /* Template Name: Work Page */ get_header(); ?> <p>This is the work 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(); ?>

Before this lesson, everything was ok except the permalink that i couldn't change from default to post name (everything went wrong when changing to post name). I'm sure my code is exactly the same as the project files but one thing that i did different from the instructor is that i used Appserv instead of XAMPP.

pls. help me figure out this problem.

2 Answers

Kevin Korte
Kevin Korte
28,135 Points

Don't forgot you have to create a new page in the WP admin, and than in the side meta boxes where there is a template dropdown, you should see your "Work" template there. Select that and than click on the update button. WP will otherwise ignore this file without that association.

Tat Angwattanapanich
Tat Angwattanapanich
2,566 Points

Thank you for your answer but i already have the meta box in style.css. Moreover, in http://localhost:8000/wordpress/wp-admin/themes.php , the templates show up and i already activated the one that i used. So, i don't think i have trouble displaying the template.

Kevin Korte
Kevin Korte
28,135 Points

And you have a custom post type only named 'work'?

I am having this problem as well. I'm pretty sure I have the code exactly like Zac does in the video and I made sure that my custom field was there and the spelling was the same in the admin area as well as my code and my work page is set to the work.template but the posts do not display

all i see is This is the header.

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

There are no posts or pages

This is the footer

This means that the correct template is pulling but my loops is not working.

here is my code

          <?php
/*
Template Name: Work
*/
 get_header(); ?>

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

<?php 
$args = array (
    'post-type' => 'work');
$the_query = new WP_Query($arg)
 ?>


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

<?php endwhile; else: ?>

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


<?php endif; ?>

<?php get_footer(); ?>
            ``` 

any ideas?

I missed the s on $args ... ---___--- wow.

Kevin Korte
Kevin Korte
28,135 Points

I just saw that too. I was going to point that out.