Ex04 Part 2: Adding the loop for dynamic content

Part 2: Adding the Loop for Dynamic Content

Background

The Loop is PHP code used by WordPress to display the content of pages and posts created in the WordPress dashboard and stored in the MySQL database. The Loop is placed in index.php and in any other Templates used to display dynamic information.

The Loop is an if/else statement. Using php, WordPress asks the database if there are any pages/posts. If and while there are if, display some information about each one; otherwise display a message or show nothing at all.

Here is an outline of the parts of the loop:

  • if ( have_posts() )
  • while ( have_posts() )
  • the_post();
  • // Post Content here
  • end while
  • end if

The information that is displayed for a particular post is controlled by the Template Tags contained within the Loop. Template tags can include the title of the post/page the_title(); the content of the post/page the_content(); categories or tags associated with the post the_category(); and the_tags(); and so on.

> See this page for more on Loops
> Full details on creating custom loops can be found within the WordPress Codex.

Instructions

  1. From your local ex04 theme folder, Open index.php. Paste the below code replacing the <article> tags, and the old hard-coded content. Upload edited index.php to the server.
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <h1 id="post-<?php the_ID(); ?>">
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent link to <?php the_title(); ?>"><?php the_title(); ?></a></h1> <article> <?php the_content(); ?> <?php wp_link_pages(); ?> <div><?php comments_template();?></div><!--delete this div to remove commentblock --> </article> <?php endwhile; ?> <div><?php posts_nav_link(); ?></div><!-- .navigation --> <?php endif; ?>

Continue to Part 3