I’ve been reading a lot about using WordPress as a Content Management System (CMS), so I started researching a bit more. A CMS fits perfectly with my idea to change the frontpage to make it a bit more interesting for my visitors. I’m pretty happy with the way it turned out, go take a look and let me know what you think. Instead of just the latest post, it is now an entry point to the entire site.
My experience with this was a bit of a turndown. I expected it to be easy to create an index of my categories with a few posts from each of them. But it did take me longer than expected. WordPress is a great blogging platform and it makes it incredibly easy to customize and theme it. However, as it turns out, this has the disadvantage of making it a somewhat more complicated to do the more advanced stuff.
As a software developper, the one thing that bothered me the most is the use of global variables and sideeffects everywhere. For instance, inside “The Loop”, the method query_posts() will influence how other methods like the the_title() react. The results are, at first, very confusing if you want multiple loops. That’s why there is a large section dedicated to it on the loop page. In this regard, get_posts() seems to solve a few problems, but until recently it had a different syntax than query_posts() (not anymore, which is nice).
Before I show you the code, a few more things I ran into:
- There is no way to loop over all categories. Unless you add a hardcoded list of categories, the only way to do this is take the output of wp_list_categories and parse that to extract all categories. That’s where the regular expression magic comes into play.
- If you query for posts the “category_name” parameter takes the “slug” or short name, not the “normal” name of the category. This is not clear in the codex.
- (not in this part of the code, but) There’s an annoying bug you might run into if you want to query for posts on both categories and tags. It’s easily circumvented though.
My next experiments:
- what’s the database load of this new frontpage? I might need to take a look at caching.
- allow multiple people to post in different categories. I’m not sure how fine-grained the access control is, but lets find out.
And finally, here’s the bit of code that will get all your categories and show the most recent post from every category. Just copy and paste it in one of your template files to see how it looks (index.php, page.php, etc.)
', $categories);
foreach($catArray as $categoryWithLink) {
if(trim($categoryWithLink) == '') break;
ereg('^
(.*)$', trim($categoryWithLink), $regs);
list($categoryWithLink, $categoryLink, $categoryShortName, $categoryDescription, $categoryName) = $regs;
?>
=$categoryWithLink?>
query('category_name='.$categoryShortName.'&showposts=1');
while ($catLoop->have_posts()) {
$catLoop->the_post();
</a>
