ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Using WordPress as a Content Management System, Part 2

(Page 2 out of 3)

The Article Section

The article section is a bit trickier than the news section, especially because articles can have multiple pages. But first, just like the news section, create a new category called 'Articles' that will hold all of our article entries.

An article usually has two bits of data: a description and the article text. The 'excerpt' part of the entry will be used for the description, and that's precisely the reason why we made the change just now in the Main Template. The full article shouldn't be shown on the index page, only the description.

Let's have a look at multi-paged articles. A great little-known feature of WP is that it does in fact already support multi-page entries. To separate an article in multiple pages, all you have to do is to include the following tag in the article:

< !--nextpage-->

A three-page article would look something like this:

Page 1

< !--nextpage-->

Page 2

< !--nextpage-->

Page 3

(Note the space between < !-- in the above examples. These are incorrect, and must be removed when you use them)

Unfortunately, WP doesn't support naming of pages, which is a real shame, because most of the time a nice content index and named pages is very useful to the reader. That's why we're going to add it, using a few clever template hacks.

To name our pages, we must add a page index somehow. To do this, we're going to add a custom field to our articles, and name each page. Adding custom fields can be done by going to the 'Advanced Editing' screen. Add a new custom field called 'index', and as value, enter each page name on a new line, like so:

Name for Page 1
Name for Page 2
Name for Page 3

Then save the custom field, and your page index has been now been saved. All we need to do now is to create a custom template for articles, and extract the page index.

To create a custom template for articles, we must first edit the 'Post' template to use our custom template. Find the following code in the 'Post' template:

if (have_posts()) : while (have_posts()) : the_post(); ?>

And replace it with:

if (have_posts()) : while (have_posts()) : the_post();

// Which category?
if (in_category(3)) {      // Article:
        include (TEMPLATEPATH . '/article.php');
        die();
} elseif (in_category(4)) {     // FAQ:
        include (TEMPLATEPATH . '/faq.php');
        die();
}

?>

This code checks whether the entry is in category 3 (my article category) or category 4 (my faq category - will be discussed later), and then gets that custom template.

Now copy the following code: article.source.phps, and save it as article.php in your template directory.

If you now go to an article, you will notice that the page names are being used in the page links, and that there is a page index (albeit very ugly positioned, but that's nothing more than a design issue). The following code in article.php actually does all the index work:


// Get article index (if available)
$index = get_post_meta($post->ID, 'index', true);

// Available?
if (!empty($index)) {
        $index = explode("\n", $index);

        // Current page?
        if ($page <= count($index)) {
                $current = $index[$page-1];
        }

        // Previous page?
        if ($page-1 <= count($index) AND $page-1 >= 1) {
                $previous = $index[$page-2];
        }

        // Previous page?
        if ($page+1 <= count($index)) {
                $next = $index[$page];
        }

        // Total pages:
        $total = count($index);
}

?>

It gets the custom field we added, and then gets each page name, and sticks it in an array. Simple and a bit crude, but perfect for our needs!

That's about it for the article section, and again it's possible to add a custom category template (recommended), but I'm sure you know how to do that by now. Let's have a look at the final section, the FAQ section.

« Previous: Introduction & News Section
Next: FAQ Section & Conclusion »



2 Responses to “Using WordPress as a Content Management System, Part 2”

  1. » Ссылки за 15.11.2005 « Заметки веб-разработчика « ZOOB Says:

    […] PHPit - Totally PHP » Using WordPress as a Content Management System, Part 2 […]

  2. WordPress TestBlog » Blog Archive » some good-looking articles abt WordPress as CMS Says:

    […] part 1 | part 2 […]

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & News Section
  2. Article Section
  3. FAQ Section & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file