Using WordPress As A CMS

I’ll let you into a little secret. WordPress is a CMS. Arguably it’s a simple one, but it’s still a CMS; I’m currently writing my content and using WordPress as a system to manage my content. WordPress needn’t just manage content for my blog though. With pages and page templates you’ve got a quick and easy way of making a killer content management system.

In this post we’ll look at how to understand just how to use WordPress “as a CMS” effectively and efficiently.

Understanding posts and pages

This is key. Once you’ve got this it’s easy to understand how you can build more complex sites quickly. Posts are for blog posts, pages are for “static content”, something else I’ve never understood; just because it’s on a page doesn’t mean it can’t be dynamic; that’s what makes WordPress such a fantastic CMS: you make “static” pages and then fetch other bits of your content dynamically.

Essentially, posts are for blog posts and pages are for everything else.

Creating ‘dynamic’ ‘static’ pages

WordPress’ pages support things called page templates which are essentially special templates or designs which you can apply to each page. These make your ‘static’ pages ‘dynamic’ as you can then use the loop amongst other things to fetch your posts, perhaps using custom fields to get only the specific info you need.

Creating a page template is easy: just add

<?php /* Page Template: Name */ ?>

to the top of your file and it’ll be available immediately as a page template once you’ve uploaded the file into your theme’s folder. You’ll find these on the page editor page, by default on the right hand side. Choose it and save and your page will immediately have the specific page template, not the page.php file which pages have if there’s no page template set.

As I said earlier, your pages can have dynamic content, specifically anything that can run in a normal template file. That means loops (via a custom query) which you can then use to grab content off your posts (like custom fields), but we’ll come to that in a sec. First the custom loop:

<?php
    $customQuery = new WP_Query();
    $customQuery->query('posts_per_page=5');
?>
<?php while ($customQuery->have_posts()) : $customQuery->the_post(); ?>

<!-- Do query stuff here -->

<?php endwhile; ?>

This is just a simple loop that gets the five most recent posts, but you’ve got all the power of the loop at your disposal so can use any of the parameters to perhaps get posts with the custom field “show-on-page” by replacing posts_per_page=5 with meta_key=show-on-page. You can learn about the basics of WordPress theme design here.

Real-life application

We’ve now got a hypothetical shop and want staff to be able to add to a page the latest offers. Each offer is a post but we only want one line of text briefly describing the offer. We’ve built a custom meta box to allow an easy way of adding custom fields to posts. The custom field contains information about our latest offers.

We want to use a custom page template, custom loop and custom field in order to display a little section about the latest offers. We also want to show “no offers at the moment, check back later!” if there aren’t any offers set. Here we’re combining everything, with a little custom fields added in extra.

The full code could look something like this (of course this is in a custom page template!):

<?php
    $customQuery = new WP_Query();
    $customQuery->query('meta_key=show-on-page');
?>
<?php while ($customQuery->have_posts()) : $customQuery->the_post(); ?>

<?php $offer = get_post_meta($post->ID, 'show-on-page', true);
if ($offer) {
?>

<p class="offer"><?php echo $offer; ?></p>
<?php } else { ?>
<p id="no-offer">Sorry! No offers on at the moment, check back later!</p>
<?php } ?>

<?php endwhile; ?>

And that’s it. Hopefully now you’ll understand exactly how you can use WordPress to power just about everything! There’s really nothing complicated about making everything easier to use! Do share if you’ve got an ingenious application for this.

Image by Wordle.