Dissection of a WordPress theme: Part 4

May 20, 2005 | Tags: guide, htaccess, meta-data, php, plugin, redirect, theme, windows, wordpress | Written by Administrator

In previous guides we have concentrated on the building blocks of a WordPress theme. A basic design structure has been defined, followed by enclosing header and footer elements, and finished off with a navigational panel. While important aspects of any blog, they are secondary to its main purpose: the content.

In this fourth and final part we carefully dissect the process of taking content from WordPress and arranging it on screen. Attention is paid to the alternative methods of grouping this information, from the multiple posts of the front page, to search results and archives.

We will look at how WordPress distributes responsibility for this work, and how everything is tied together with the all-seeing all-knowing construct known as ‘The Loop’.

By the end of this guide we should have a fully working theme and enough WordPress experience and knowledge to extend our theme beyond the basic design presented here.

The Loop

The Loop is the front-facing side of WordPress and sits at the top of the code hierarchy. Its basic role is to obtain information about posts that can then be displayed easily and conveniently.

It is called ‘The Loop’ because it is, very simply, a loop – there is no hidden trickery here. Each iteration through The Loop causes WordPress to load the post information, which is then accessed through special template functions. The Codex describes most of these functions in great detail.

The primary loop exists within index.php, but the loop can also be found in several other of the files contained within a theme. We will look at index.php in more detail shortly, but for now the loop can be extracted as a very short piece of PHP code:

while ( have_posts() ) : the_post();
  code
endwhile;

Yes, that really is it. All the underlying database work and the multitude of PHP files all boil down to whatever exists between these two lines. Breaking this down even further we have two WordPress function calls wrapped within a PHP while statement - PHP will execute whatever is between the colon and endwhile, so long as the condition is true.

In the above code, the condition is the first WordPress function:

have_posts ();

This is a simple function call that returns true if we have a post to display, or false if we don’t.

How does WordPress know whether we have a post to display? It deduces this from the URL of the current page. On the front page we may have several posts to display, while a single page will only have one. Regardless of the context, The Loop is fundamentally the same, and WordPress will stop the loop at the correct time by causing have_posts to return false when there are no more posts. Execution then resumes after the endwhile.

The second essential part of The Loop is:

the_post ();

You can think of this as being the function that tells WordPress to load the information for the next post and prepare it for our use. Each subsequent call of the_post will cause WordPress to give us the next post, and the next, and so on – it is basically a pointer that steps through the posts.

For example, if we have ten posts to display then have_posts will be called 11 times – the first ten will return true and the eleventh will return false. Each time it returns true, the_post will be called to get the information about the post.

Anything else following the_post and before the end of the loop is entirely our choosing, and is what we use to display the post information – it can be as basic or advanced as required.

Delegation of duty

WordPress provides many ways for us to display a collection of posts. For example, the front page contains the latest posts. The archive section will show a larger collection of posts, spread over a specific time period. The search page shows posts that meet search conditions.

Rather than having one super-sized piece of code that manages all of this, the work is broken down and delegated to several smaller handlers that deal with a specific task. Each of these will contain the exact same loop discussed above, but will process the information in different ways.

The handlers are spread across different files:

index.php – displays the front page single.php – displays a single page search.php – displays search results archive.php – displays archives page.php – displays a WordPress ‘page’

While this is very flexible, it does cause one problem: duplication. If we wish to maintain a consistent theme across our entire blog then we will need to duplicate it in each of these files. Fortunately most of the changes will be stylistic, and will only require a CSS modification. A basic structural change can cause a lot of work.

It should be noted that in previous guides we did in fact introduce a basic structural change. This means we need to update each of the files - an ideal reason to investigate everything.

Help me to save time by reading these instructions!

If you are asking a question please read the FAQ to see if it has already been answered. All support questions should be directed to the support forum. Thanks!

Share This

Comments (page 7 of 7)

author
Royce Tivel :

May 14, 2008 2:10 pm

Hi, John,

I am exploring the possibility of using WordPress 2.5 for my site and was delighted to discover your 4-part article. The article clarified my understanding of WordPress theme design enormously. Thanks!

About the only problem I had with my first read and implementation on my Apache testbed was the references to the .entrytext class. This has apparently been changed to .entry in the latest version (for example, in the code for the single.php file show below). Changing .entrytext to .entry where it occured made everything come out alright.

(page 4 part 4)
.entrytext .postmetadata
{
font-size: 1.1em;
background-color: #FDE5C3;
width: 70%;
margin: 2em auto3.5em auto;
border: 1px dotted #e9b17b;
padding: 5px;
padding-left: 45px;
background: #FDE5C3 url(images/metadata.png) no-repeat scroll top left;
}

Again, thanks for the great articles.

Royce

author
John (author) :

Jan 10, 2008 12:25 am

Patrick, the time and date is typically displayed with something like this:

<?php the_time('l, F jS, Y') ?> at <?php the_time() ?>

Just delete this from your theme.

author
Patrick :

Jan 6, 2008 7:17 pm

I do not want a date or time to appear below any of my posts (I'm using WordPress as my CMS). What must I delete and/or modify, and on which pages? Thanks for a useful and well-written guide.

author
John (author) :

Sep 9, 2007 11:44 pm

Gijet, your code disappeared so I'm not sure what you are asking

author
Gijet :

Aug 30, 2007 2:37 pm

Hi...

John, thanks alot for your article. I used theme &gt;<a href="" rel="nofollow">

I've changed code above with kubrick navigational code to replace the Older Entries and Newer Entries text with post/article title. Like below :

<code></code>

I got above code in kubrick's single.php file. But the output is disappear. I've read the tut on http://codex.wordpress.org/Template_Tags/posts_nav_link and implemented some guidance on that but still can't change the older Entries and Newer Entries text to post/archive title.

So, please tell me the way. Thanks</a>

Pings & Trackbacks

benjoblog.weblogs.us, bingu.net, blog.23corner.com, blog.websurf.ro, cssquestions.wordpress.com, de.eugens-web.de, lorelle.wordpress.com, r0x0rz.info, tekapo.com, tinyau.weblogs.us, vanus01.host.sk, wordpr.ectio.us, bloggingpro.com, clydeshome.net, daveschalkboard.com, grantpalin.com, marketingloop.com, netlexfrance.com, ohisee.com, shaghaghi.net, shsh.ylc.edu.tw, tjeerdoo.com, tyic.com, xxiaa.net

Leave a comment


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Help me to save time by reading these instructions!

If you are asking a question please read the FAQ to see if it has already been answered. All support questions should be directed to the support forum. Thanks!

Home | Software | Terms & Conditions | Sitemap | John Godley © 2008
Close
Social Web E-mail
E-mail It


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser