PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Control Your Content Part 2

Posted by on 13th Jul 2008 | 3 comments

Last week I explained the 3 content tags, which you can use either in your WordPress template files or whilst writing your post.

So how can you use these to create a different front page? Using a simple if statement you can determine which post you’re displaying and alter your settings to suit. For example, display your most recent post in full, then the rest of the posts in excerpt format.

Using The Loop

The WordPress Loop is used to display the post(s) or Page content on the selected page. It first checks if there are posts (or a Page) and then loops through the results and displays them using various template tags, a bit like a page of search results. The loop from the Classic theme is

[sourcecode language="php"]< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>

< ?php the_date('','

‘,’< /h2>‘); ?>

< ?php _e("Filed under:"); ?> < ?php the_category(',') ?> — < ?php the_tags(__('Tags: '), ', ', ' — '); ?> < ?php the_author() ?> @ < ?php the_time() ?> < ?php edit_post_link(__('Edit This')); ?>
< ?php the_content(__('(more...)')); ?>

< ?php comments_template(); // Get wp-comments.php template ?>

< ?php endwhile; else: ?>

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

< ?php endif; ?>[/sourcecode]

The main part of this code that we want to concentrate on is the part that controls the actual post content which is

[sourcecode language="php"]

< ?php the_content(__('(more...)')); ?>

[/sourcecode]

To make this switch between the_content() and the_excerpt() we need to use an if statement. We will also probably want to only have the full post on the first page and not subsequent pages, or category/archive pages (if they run off the index.php file, remember the Template Hierarchy).

So first off we set up a simple counter so we know which post is being displayed. Before the loop is displayed we initialise the variable (mainly to avoid errors and to be complete). ie.

[sourcecode language="php"]< ?php $pc = 0;
if (have_posts()) : while (have_posts()) : the_post(); ?>[/sourcecode]

So now we’ve got our post count variable – $pc. Then around the_content() we change this to check if the post being displayed is the first post and we’re on the home page. If both of those are true then we use the_content() otherwise we use the_excerpt().

[sourcecode language="php"]

< ?php
if ($pc == 0 && is_front_page()) { // checks if its the first post on our front page
the_content(__('(more...)'));
} else {
the_excerpt();
}
$pc++; // this then increases the post count variable by 1
?>

[/sourcecode]

Notice the last line of PHP code – $pc++;. This comes after we’ve displayed the post, and it just adds 1 to the variable. So on the second loop around $pc would be equal to 1 and the excerpt would then be displayed, and continue to be displayed.

You could also add additional markup around the_content() to allow you to then style the first post differently from the rest, perhaps a different background colour, or add a border to it eg.

[sourcecode language="php"]echo “

\n”;
the_content(__(‘(more…)’));
echo “

\n”;[/sourcecode]

Magazine Style

I’m sure many of you have seen or heard of the ‘Magazine style’ themes that have become quite popular amongst bloggers. This is often a style where you have the most recent post in full then the last X posts listed or to the side with just their titles.

You may think to restrict the posts listed (via your settings) to just 1 and then use the get_posts() to list the last X posts using an offset of 1. However, why use two different sets of code when just using the same bit of PHP in the WordPress loop will do. Of course your design may require the first method, however it’s advisable not to use this as your archive and category pages will also list 1 post per page, which obviously isn’t as desirable.

So, to achieve this, we just need to expand our if statement to cover more of the code within the loop from above. We can keep the code for the first post, but perhaps change it to just list the next X posts in an unordered list below it, i.e.

[sourcecode language="php"]< ?php $pc = 0;
if (have_posts()) : while (have_posts()) : the_post();
if ($pc == 0 && is_front_page()) { // checks if its the first post on our front page
the_date('','

‘,’< /h2>‘); ?>

< ?php _e("Filed under:"); ?> < ?php the_category(',') ?> — < ?php the_tags(__('Tags: '), ', ', ' — '); ?> < ?php the_author() ?> @ < ?php the_time() ?> < ?php edit_post_link(__('Edit This')); ?>
< ?php the_content(__('(more...)')); ?>

< ?php comments_template(); // Get wp-comments.php template

echo "

    “;
    } else {
    echo “
  • “.the_title().”
  • \n”;
    }
    endwhile;
    echo “< /ul>\n”;
    else: ?>

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

    < ?php endif; ?>[/sourcecode]

    So in the above code, on line 3 I’ve added in our ‘if front page and first post’ check. If this qualifies then the standard post method is used. Then notice on line 23 I open up the unordered list tag ready for the list of older posts. Line 25 then prints out a list with a link through to each older post. Finally on line 28 the closing unordered list tag is printed out. This comes after the closing while loop. so all the posts for the page will have been looped through.

    These are just a couple of ways of how you can manipulate your output to give the front page of your blog a bit more of a non-standard look.


A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

3 comments - Leave a reply