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 trialdanielcroft
7,438 PointsDisplaying Custom Posts and Fields in a Template
I am struggling to see where my code has gone wrong on the "displaying custom posts and fields in a template" video as its not showing the titles like zac shows after entering the $the_query My code: <?php /*
Template Name: Work Page
*/
get_header(); ?> <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_content(); ?> <hr> <?php endwhile; else: ?> <p> There are currently no posts </p> <?php endif; ?> <?php get_footer(); ?>
Hope someone can help.
Thanks, Dan
Alexander Sobieski
6,555 Points(this lets you define the title as a title, and use CSS to enhance the aesthetics)
danielcroft
7,438 PointsI dont remember him using the html tags around it in the video
Alexander Sobieski
6,555 PointsDid he do this?
<?php the_title( '<h1 class="post-heading">', '</h1>' ); ?>
You can include the html as args in the function too
4 Answers
danielcroft
7,438 Pointshttps://www.dropbox.com/s/2j83dvl2pyhs41q/zacscode.png
Thats Zacs code for this hes got it in h3 tags but so have i
//<?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 currently no posts </p> <?php endif; ?>
Alexander Sobieski
6,555 PointsHey Daniel Croft-Bednarski -- When you post code to the forum, you can hit the Space Bar 4 times, for each line of code, and it gives perforated code blocks, so we can read your code better (and so parts of the code don't disappear).
Examp:
<h2>here's a heading 2 with no spaces</h2> <strong> here's a "strong" tag </strong>
<!--here they are with 4 spaces before each line-->
<h2>here's a heading 2 with 4 spaces</h2>
<strong> here's a "strong" tag </strong>
--I just mention this because sometimes the forum software eats parts of the code and it's hard to tell what's going on.
Alexander Sobieski
6,555 PointsHaving said that, are you still having problems or did it resolve when you added h3 tags?
(if it's still not working, what's not appearing correctly?)
Curious to know if it was just the html
danielcroft
7,438 Points<?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>
<div><?php the_field('description'); ?></div>
<hr>
<?php endwhile; else: ?>
<p> There are currently no posts </p>
<?php endif; ?>
Ok so this is the code im trying neither the header of the content is showing.
Thank you for trying to help me
danielcroft
7,438 PointsHmm is still seems to be making half my code disappear even with the 4 spaces ill come up with a solution in a min. http://codepen.io/anon/pen/mrpuF Ive put the code in here
Alexander Sobieski
6,555 PointsYEah, this forum software is a bit picky. You have to hit 4 spaces in front of each line (which usually works, but sometimes not).
Your codepen version is missing this <?php get_header(); ?>
One thing to make sure is that you're editing the same php file that your browser is trying to access. (also, make sure your browser cache has been cleared and that you've refreshed)
If you have a template file that does work (for example, single.php) try copy/pasting it into the work page file. //make sure to add this <?php /** * Template Name: Work Page **/ get_header(); ?>
If that works, replace the code you copied with the custom query, and you'll find the offending code.
I'm curious to know what it ends up being
danielcroft
7,438 Pointshttp://codepen.io/anon/pen/mrpuF I have added the header in now im not sure what you mean by what you said there ive cleard all that and its still the same is the code correct?
Alexander Sobieski
6,555 PointsNot sure... maybe Zac Gordon can be of help
Zac Gordon
Treehouse Guest TeacherHey all, I will have all the code samples up on the site later today.. hopefully that will help with this (and a few other issues students have had)!
danielcroft
7,438 PointsOk that would be great, thanks for that Zac :)
Zac Gordon
Treehouse Guest TeacherOkay, they're live! Check them out and let me know if it resolves your problem
Alexander Sobieski
6,555 PointsAlexander Sobieski
6,555 PointsI haven't done the WP modules yet, so I'm not sure what the directions are for this section -BUT- you don't seem to have HTML tags around your content and title.
When I develop WP themes, it looks something like this: (for example only)