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 trialAngelic Sanoy
Full Stack JavaScript Techdegree Student 6,743 PointsWordPress Loop Statment
I have stupid question. Starting to learn wordpress for php. Why is it the the_title and the_content is outside the closing php of if statement?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h1><?php echo the_title(); </h1> <p> <?php echo the_content(); </p>
<?php endwhile; else : ?>
1 Answer
Dave StSomeWhere
19,870 Pointsthe_title()
and the_content()
template tags are missing their closing tags, they do have the starting tags. (It is possible that it could still work without the closing tags, but, if it was my code, I'd make sure they were there)
The key is to notice the many opening and closing tags and even the use of PHP alternative syntax for control structures.
With the closing tags, I would code it as follows:
<?php if ( have_posts() ) :
while ( have_posts() ) :
the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<p><?php echo the_content(); ?></p>