Custom Post Loops
Difficulty: Advanced (This is a tool for WordPress theme developers, not necessary for normal users)
This makes is possible to replace any post loop in any PliablePress theme, with one of your own.
And whilst I’ve said at the top that this is advanced, it’s actually simple. The advanced part is writing your own loop and making use of it!
If you aren’t sure what a loop is yet, it is the part of your page that displays your posts. You can read more in the Codex.
How to Create Your Own Loop
All of the loops in our themes are now inserted using the WordPress 3.0 template tag, get_template_part().
The easiest way to explain it to you is to take an example. Let’s say you wanted to replace the articles on the homepage of Creative Soul with a single post in full.
Using get_template_part(), WordPress is going to look for the following theme files, in this order:
- creative-soul-blue/loop-home.php
- creative-soul-blue/loop.php
- chameleon/loop-home.php
- chameleon/loop.php
By default, the only file in that hierarchy which it will find is the 4th one. Therefore, if you create a new file in your creative-soul-blue directory called,
creative-soul-blue/loop-home.php
, then WordPress will use that instead, and ignore the loop.php file in the homepage. And there are no limits at all to what you can put in the file. So for our example above, you’d write a loop that displayed just the latest post.
Now, let’s take it a step further. Let’s say you wanted to replace all of the loops with one loop (And perhaps use conditional tags in it), then you could create a loop.php file in the creative-soul-blue directory, and now every loop in Creative Soul will use that file.
What Loops Are Available?
The following are all of the available types of loop that you can override:
- Home – loop-home.php
- Single Post – loop-single.php
- Author – loop-author.php
- Page – loop-page.php
- Index (Tags and Categories) – loop-index.php
- List (e.g. the small list of posts in the Ply Recent Posts widget) – loop-list.php