Sunday, August 26, 2012

The Anatomy of a WordPress Theme – Dynamic Content

Slippery eight loop

Slippery eight loop (Photo credit: Wikipedia)



Now that all of the standard template files have been created, and the header, footer, and sidebar can be dynamically included into any template file using standard WordPress PHP variables, it’s time to pull the actual WordPress content into the templates for display to the end user. This is done on almost every page by using a variation of the so-called “WordPress Loop.”


Without using database queries and advanced pieces of code, one WordPress Loop can be included per template file when displaying post content, the dynamic content assigned to pages from within the WordPress Dashboard, and creating single category, archive, and tag pages. The Loop is even used in the comment template to pull entries from the database, and a second “Comment Loop” is used to pull the associated interactions with that entry from a linked database table.


The WordPress Loop always starts with the code below, which simply checks the database to see if there are any entries to display based on the template hierarchy currently in action. That means the Loop will automatically look for certain categories, date ranges, or tags, based on the permalink a user has clicked. Here’s how that conditional looks:



<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

Next, template developers can begin calling information from the database and printing it on the page by using standard WordPress PHP variables. These can be used to display things like the entry title, the full content or an excerpt of the content, tags, categories, the date, the author’s name, and permalink information for inclusion in an <a>tag. Here’s a very basic example of a WordPress Loop pulling entry information using these variables:



&lt;a href="&lt;?php the_permalink() ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;br/&gt;
Posted on &lt;?php the_time('F D y') ?&gt;
&lt;p&gt;&lt;?php the_content(); ?&gt;&lt;/p&gt;

Now that the appropriate information has been pulled out of the database using these simple WordPress PHP variables, the WordPress Loop must be closed. This process involves not only bringing an end to the built-in database queries, but also using another conditional statement to print a brief error message to readers if no content is found for display within the Loop. This will happen if a user visits a category page for which no entries have been assigned, or if they navigate to an archive month during which no entries were posted. Here’s how the end of the Loop looks in most standard WordPress templates:



&lt;?php endwhile; ?&gt;
&lt;?php else : ?&gt;
&lt;?php _e("Unfortunately, there are no entries to be displayed here. Please try a different link."); ?&gt;

The php_e variable above is simply an instruction that an error message should be displayed to users who find an empty Loop on any of the site’s dynamic template pages. The text within the parentheses can be customized to a template developer’s liking, and the tag can be surrounded by standard XHTML elements in order to customize its display via CSS code in the style.css file which was created and discussed earlier in this primer on theme anatomy.


This is a part of The Anatomy of a WordPress Theme tutorial.



The post The Anatomy of a WordPress Theme – Dynamic Content appeared first on Tutorial Mini.


Related posts:



  1. The Anatomy of a WordPress Theme – Theme Files and Typical File Structure

  2. The Anatomy of a WordPress Theme – Theme Location

  3. The Anatomy of a WordPress Theme






via Tutorial Mini http://tutorialmini.com/the-anatomy-of-a-wordpress-theme-dynamic-content/

No comments:

Post a Comment