Home > CMS > Very simple CMS: project category

Very simple CMS: project category

November 24th, 2009 Marius Leave a comment Go to comments

I made this blog to have a place to post the applications and programs I have made, and for that I needed a Content Management System (CMS). I looked at some CMS plugins for WordPress, but most of them are overkill for what I need. Instead I decided to use the built inn Categories of WordPress. I created a category called Project, and for each project (a program, website or anything else I made) I would add a sub-category with that projects name. With this simple method I could create several posts describing the progress of a project. Now all I needed was a page with all the projects listed, and maybe the latest posts for each one.

I solved this by making a page Template (not to be confused with Theme, which is something completely different). The template lists all the subcategories of a category, specified in the page metadata, along with the category description and the last 5 posts for each category. This way you can list all the projects on one page and link to the most recent information for each one. Because the category name is specified in the page metadata, you can have multiple CMS pages, for example, ideas, projects or experiments.

Add the template file to /wp-content/themes/<active theme>. To use this template, make a new page, then select the template called ‘CMS’ from the list of templates. The content will be shown at the top of the page. Next you need to add a metadata (also called custom field) to the page, with name ‘category’ and value equal to the category you want the page to show. You might need to edit the template slightly for it to display properly with your theme.

<?php

/*

Template Name: CMS

*/

?>

<?php get_header(); ?>

<?php if (have_posts()) : the_post(); update_post_caches($posts); ?>

  <div class="post" id="post-<?php the_ID(); ?>">

    <h1><?php the_title(); ?></h1>

    <div class="content">

      <?php the_content(); ?>

      <?php if(count(get_post_custom_values('category')) > 0 ): ?>

        <?php $category_slug = get_post_custom_values('category'); $category_slug = $category_slug[0];?>

        <?php if(($category = get_category_by_slug($category_slug)) != null): ?>

          <?php $categories = get_categories(array('child_of' => $category->term_id, 'orderby' => 'name', 'order' => 'asc')); ?>

          <?php foreach($categories as $category): ?>

            <div class="post">

              <h2><a class="title" href="/category/<?php echo $category_slug."/".$category->category_nicename; ?>"><?php echo $category->name; ?></a></h2>

              <div class="content">

                <p><?php echo $category->description; ?></p>

                <!--Latest post -->

                <?php $posts = get_posts("cat=$category->term_id&numberposts=5&order=DESC&orderby=date"); ?>

                <h5>Latest posts</h5>

                <?php foreach($posts as $post): ?>

                  <?php setup_postdata($post); ?>

                  <div class="content">

                    <a class="title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>

                    <span style="float:right" class="date"><?php the_time(__('F jS, Y', 'inove')); ?></span>

                  </div>

                <?php endforeach; ?>

              </div>

            </div>

          <?php endforeach; ?>

        <?php else: ?>

          <div class="errorbox">

            <?php echo("Category '$category_slug' was not found!"); ?>

          </div>

        <?php endif; ?>

      <?php else: ?>

        <div class="errorbox">

          <?php echo("Add a custom field with name 'category' and value equal to the name of a category"); ?>

        </div>

      <?php endif; ?>

    </div>

  </div>

<?php else : ?>

  <div class="errorbox">

    <?php echo('Sorry, no posts matched your criteria.'); ?>

  </div>

<?php endif; ?>

<?php get_footer(); ?>
Categories: CMS Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Marius Gundersen is Digg proof thanks to caching by WP Super Cache