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 trialardon Bailey
3,750 PointsDisplaying Custom Posts and Fields in a Template issue again
My work posts are not displaying on the page. Any help is appreciated. Thanks!
<?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><?php the_title() ;?></h3>
<?php the_content(); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
<?php get_footer(); ?>
6 Answers
Matt Campbell
9,767 PointsHi ardon Bailey
Take the spaces out of this
$args = array( 'post_type' => 'Work' );
between type' => 'Work so it looks like this:
$args = array( 'post_type'=>'Work' );
ardon Bailey
3,750 PointsThanks Matthew Campbell , I fixed that. Now, when I click on the work link, it says "this is the page.php." Not too sure where I went wrong with my code.. Here is the page.php :
<?php get_header(); ?>
<p> This is the page.php file </p>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
<?php get_footer(); ?>
Matt Campbell
9,767 PointsNothing wrong with the code.
Are you wanting it to load a different page template when you click on the work link...say work.php?
If so, what you'll need to do is put this code into a work.php file with a comment at the top of it that reads like this.
<?php //Template Name: Work ?>
Now, when you are linking to the work page, I'm assuming its being done through a menu, when you create the page in Wordpress you'll have the option to select a template on the right hand side under the publish metabox.
This will now load work.php instead of page.php.
Does that help or have a got the wrong end of the stick?
ardon Bailey
3,750 Pointshmm child pages of work aren't showing up now. I'm going to redo it and see if that fixes the problem. Thanks again for the help.
ardon Bailey
3,750 PointsI redid the lessons and here's where I'm stuck at http://listentotheway.com/work/ . Also, I'm not sure if I must of missed one of the lessons but to get posts under the work page, am I creating subpages or click on post and making a category called "work?" I still have the same code above but I'll post it again.
<?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(); ?></h3>
<?php the_field( '' ); ?>
<hr>
<?php endwhile; else: ?>
<p> customize later </p>
<?php endif; ?>
<?php get_footer(); ?>
Zac Gordon
Treehouse Guest TeacherHi folks! Did you go into your Work page in the admin area and select the Work Page template?
ardon Bailey
3,750 Pointsyessir
Zac Gordon
Treehouse Guest TeacherHokay :) The work.php page is calling in any custom posts entered as Projects into the Work section of the admin area. So, you will have to have projects added, which I'm assuming you are.
I also noticed that your markup for displaying the title is missing a closing > tag. If you put some static text inside of the loop, does it show up on the Work page that you have the template selected for?
ardon Bailey
3,750 PointsI had to clean up my code and start over again and got it to work. Definitely user error on my part. Thanks for the replies. Definitely a learning experience.