WordPress as a Content Management System: How to Display a Post from Every Category

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.

Improved Inviolable Lock
Creative Commons License photo credit: alisdair

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.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
   $categories = wp_list_categories('echo=0&exclude=44&style=none&title_li=');
   $catArray = split('<br />', $categories);
   foreach($catArray as $categoryWithLink) {
     if(trim($categoryWithLink) == '') break;
     ereg('^<a href="(http.*/([^/]*)/)" title="(.*)">(.*)</a>$', trim($categoryWithLink), $regs);
     list($categoryWithLink, $categoryLink, $categoryShortName, $categoryDescription, $categoryName) = $regs;
 ?>
     <div>
       <h2><?=$categoryWithLink?></h2>
       <?php
         $catLoop = new WP_Query();
         $catLoop->query('category_name='.$categoryShortName.'&showposts=1');
         while ($catLoop->have_posts()) {
           $catLoop->the_post();
           <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
             <?php the_title(); ?>
           </a>
       <?php } ?>
     </div>
     <?php } ?>
 <?php } ?>
This entry was posted in Java and JavaScript. Bookmark the permalink. Both comments and trackbacks are currently closed.
  • Feedback or questions? Contact me right away.

    Comments have been disabled on my posts. Not because I don't want to hear from you, but because they were adding very little to the conversation (most of them were spam anyway). I do listen to you and try to keep as much posts as possible up-to-date and error free. So if you have a question, if something isn't working the way you hoped or you have general feedback, please use the contact form below. I guarantee an answer to every honest question or remark.
  • Get in touch
    1. (required)
    2. (valid email required)
     

    cforms contact form by delicious:days