Limit and Alternate

April 1st, 2005

I just did a quick Google for “limit number of posts on home page wordpress” and came to Justin Blanton’s site. That is exactly what I wanted to do. And he’s right, it’s just that easy.

But to make it even more so, I’ll tell you exactly where to put that code on your page in WordPress 1.5. I actually took an educated guess and was right on the first try. That doesn’t happen very often, so I figured it was worth a post to celebrate. I’ll also include a semantic way to have alternating colors for your posts on the index page (doesn’t it sound so much better with the word “semantic” attached to it?). Here goes:

Create A Variable
If you knew which entries were even and which were odd on your home page, alternating the colors between the two would be easy. Unfortunately, the only way to do that with WP out of the box is by using the post ID. And even then, if you delete entries, keep some as drafts, or even change the timestamp, you could end up with 2 or more odd numbered entries in a row. So how can you determine which are odd numbered and which are even numbered?

If you create a variable that will hold a value of ONE or ZERO, you could use that to help out. First things first, change <?php get_header(); ?> to <?php get_header(); $count = 0;?>. That COUNT variable will be used for the alternating colors. The value of COUNT will determine whether the entry is even or odd. Now that’s out of the way, press on, secure in the knowledge that COUNT EQUALS ZERO! Yipee.

Alternating Code
Using Presentation >> Theme Editor, edit your Main Template. To get those alternating colors going, right after <?php while (have_posts()) : the_post(); ?>, add the following:
if($count == 0)
{
$count = $count+1;
/* IF COUNT IS 0, ROWID IS "ODD" */
$rowid = "odd";
}
else
{
$count = 0;
/* IF COUNT IS 1, ROWID IS "EVEN" */
$rowid = "even";
}

So, if COUNT equals ZERO, you make it ONE instead. If COUNT equals ONE, you make it ZERO instead. This way, COUNT can only be 1 of 2 numbers and it will alternate back and forth between the two each time WP runs through the loop to post content to this page. Since the value of COUNT helps determine the value of ROWID (either “even” or “odd”), if you set your CSS to show anything with “odd” as one color and anything with “even” as another color, there are your alternating colors. Presto! One has a background color, the next doesn’t. On, off, on, off.

Limit Posts
Immediately following that snippet of PHP, paste the first bit of Justin’s code:
static $ctr = 0;
if ($ctr == "4")
{
break;
}
else
{
?>

The CTR variable equals the number of posts listed on your home page. Change as needed.

Apply Liberally
Anywhere inside the loop, call on &count to display that “even” or “odd” mentioned before. One convenient place for it is in the “post” DIV:
<div class="post_<?php echo $rowid; ?>">

Wrap It Up
Just *before* <?php endwhile; ?>, paste the last bit of Justin’s code:
<?php $ctr++; }

In order for the loop to stop once it reaches the CTR number, you’ll need to add one to that variable so it keeps track of how many times it’s cycled through.

And paste another bit to finish off your alternating colors routine:
$count+1; }

In order for the COUNT variable to switch between ZERO and ONE (thereby enabling ROWID to be either “even” or “odd”), you’ll add one to COUNT each time through. Remember your initial code to make this work? If COUNT is ZERO, you make it ONE. If it’s ONE, you make it ZERO.

Mix Well
There’s not much to add about Justin’s code. Once you have it in place, all you need to modify is that CTR variable.

For the alternating colors, you just need to add background colors for “.post_even” and “.post_odd” in your CSS to have those entries on the home page alternate. You’re all set for those alternating colors once you decide to use them.

Maybe someone can tell me why $count++ wouldn’t work here; I tried it and it wouldn’t add to the COUNT variable. Once I made it $count+1, it worked just fine. I’ve used this code on several other projects and it’s worked with $count++. Clearly, there’s something I’m missing and I am curious.

No big deal, right? To sum up, here’s what you have:

FIRST, change

<?php get_header(); ?>

to

<?php get_header();
$count = 0;?>

SECOND, change

<?php while (have_posts()) : the_post(); ?>

to

<?php while (have_posts()) : the_post();
{
/* begin alternating colors */
if($count == 0)
{
/* if COUNT is 0, add one to it (COUNT is now 1) */
$count = $count+1;
/* if COUNT is 0, ROWID is "odd" */
$rowid = "odd";
}
else
/* if COUNT is anything other than 0, make it 0 */
{
$count = 0;
/* if COUNT is anything other than 0, ROWID is "even" */
$rowid = "even";
}
static $ctr = 0;
if ($ctr == "4")
{
/* if CTR is 4, stop! */
break;
}
else
{
/* if CTR is anything other than 4, continue */
?>

THIRD, change

<?php endwhile; ?>

to

<?php
/* add one to the CTR variable */
$ctr++;
}
/* add one to the COUNT variable, too */
$count+1;
}
endwhile; ?>

Limiting the posts placed on the home page and a way to semantically alternate colors of those posts, not a bad day’s work. Well, only a few minutes, actually…

This works in WordPress 1.5 for certain (QED). I don’t see any reason that it shouldn’t work in other versions, however, as none of the code here uses WP 1.5-reliant functions or anything similar. Let me know where else this works on any other versions or if this worked for you on your site.

3 comments on “Limit and Alternate”

  1. Andrew Says:

    I did something very similar to this for my blog. The first post on all pages needed special styling, then I decided to have some fun with it and make it show the full post if it was first on the page and make it show the excerpt if it wasn’t.

    So right before the the loop starts:

    <b></b>


    then for my content:

    <a>Continue reading this entry</a>

    Now by the end of the loop I have to set the $isFirstPost to false so that it won’t repeat.

    Getting under the hood of wordpress is soo fun!
    (I hope my code samples don’t get destroyed by wordpress filters).

  2. Todd Says:

    I believe your code did get filtered out. Encase it in <code> tags and make sure all of your brackets are & l t ; and & g t ;. I need a fix for that, so I can post code easily and so can others!

    But I don’t see any special styling on your first post. What did you do to it?

  3. Jalenack Says:

    It still doesn’t show my code snippets…I think the markdown plugin can help with that, not sure though. For styling the first post, just a simple:

    <div <?php if ($isFirstPost) { echo 'id="firstTitle"'; } else { echo 'class="postTitle"'; } ?> >

    Even more exciting are my comments and customized verbs for when certain users comment.

    In /wp-includes/comment-functions/


    function get_comment_author_link() {
    global $comment;
    $url = get_comment_author_url();
    $author = get_comment_author();

    if ( empty( $url ) )
    $return = "$author thinks";
    if ( !empty($url) )
    $return = "<a href="$url" rel="external">$author</a> thinks";
    if ($comment->comment_author == "Nella") {
    $return = "$author quips"; }
    if ($comment->comment_author == "SuperDave") {
    $return = "$author digresses"; }
    if ($comment->comment_author == "Andrew") {
    $return = "$author declares"; }
    if ($comment->comment_author == "Jackie") {
    $return = "$author retorts"; }
    if ($comment->comment_author == "Sam") {
    $return = "$author grumbles"; }
    return apply_filters('get_comment_author_link', $return);
    }

    Then correct the comments template in your theme folder..It’s not a very clean hack, but with a small, consistent base of commenters, it’s nice.

    So, would you like your own personalized verb?

Have your say...

Comment Preview: