WordPress WordPress Theme Development The WordPress Loop Adding the Loop to the index.php File

Angelic Sanoy
Angelic Sanoy
3,165 Points

WordPress 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
Dave StSomeWhere
19,805 Points

the_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>