WordPress Themes .. Displaying Content

Pages: 1 2 3

Template Tags for Content

We edited the index.php file and replaced the phrase <em>HERE WILL BE THE DYNAMIC CONTENT</em> with the following code:

index.php

<hr />
<!--the loop-->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
   <h2><?php the_title(); ?></h2>
   <?php the_content(); ?>
 <?php endwhile; else: ?>
   <p><?php echo 'Oops, it looks like there are no results.'; ?></p>
<?php endif; ?>
<!--/the loop-->
<hr />

In our WordPress install, there is only one POST.  The code we inserted is pretty standard WordPress code, dealing with – what WordPress calls – The Loop.   The following WordPress functions are used:

  • have_posts()
  • the_post()
  • the_title()
  • the_content()

First, here is what gets returned to the web browser:

have_posts()

When WordPress gets the url passed, a query is executed in the background.  Since in our example, we give no parameters as part of the url, WordPress, by default, finds if there are any posts (pieces of content of post type POST) in the database.  The function have_posts() returns true, if there are any posts satisfying the query.  Our query was wide open, so it returns false only if no posts (pieces of content of type POST) exist at all.

the_post()

When this function executes, the information about the FIRST available post is made available to other Template Tag functions, which can then push HTML content to the browser.

the_title()

Displays the title of the CURRENT (as defined by the_post()) post.

the_content()

Displays the HTML content of the CURRENT (as defined by the_post()) post.

In the usual case of an existing WordPress site there will be more than one post that exists in the database.  WordPress’s the_post() function will, by default, return posts from the newest through the oldest.

Pages: 1 2 3

One Response to “WordPress Themes .. Displaying Content”

  1. […] the WordPress .. Displaying Content article we introduced WordPress Template Tags, and showed how they are used in index.php to […]

Leave a Reply

You must be logged in to post a comment.