Simple WordPress different post style hack
Many of us want to display the first post in our blog a little differently from the rest, as was a request from one of my clients recently. So, after much badgering the good folks at the WordPress IRC channel, I was able to make a simple hack for the aforementioned purpose.
This is so simple, you’ll probably end up saying Do’h!
First of all, here’s the code for you:
Now, let's take it apart.
First, we introduce a variable $i, and give it a value of 1. This has to be done before we begin the WP loop though. Immediately after the loop, we introduce an if statement which checks if the value of $i is 1, or, in layman's terms, whether this is the first time the loop is running. If it is, it will execute all the goodness that is contained within the enclosing curly braces.
After the first post is done, comes the else statement. The else statement simply states that if the value of $i is more than 1, or rather, if this is not the 1st time the loop is looping, execute the other goodness. We then simply close the brace for the else.
Now comes an important part. We now simply increment the value of $i so that with each loop the value of $i increases by 1.
And that's all there is to it. The example I've shown you is really a very simple example, and you are limited only by your imagination. For example, you may want the 5th post to have a black background. You simply change the if statement, and you're on your way.
So, I hope this is of some use to someone out there. Enjoy!
March 24th, 2006 at 12:10 am
If you only want to change the first post, I prefer using a “double” loop as I call it (although it’s really only one loop).
You start with a simple “the_post()” which sets up the first post, then follow with a normal loop: (note [ = <)
[?php if (have_posts()) : the_post(); ?]
[!-- Do the special first post stuff here. --]
[?php endif; ?]
That’s the first post seperate from everything. So now you could easily do some some stuff here, then continue with a normal loop, which will start with the second post:
[?php if (have_posts()) : ?]
[?php while (have_posts()) : the_post(); ?]
[!-- Do the mundane post stuff here. --]
[?php endwhile; ?]
[?php else : ?]
No post found.
[?php endif; ?]
Anyway, I find that a little easier to work with.
March 24th, 2006 at 12:21 am
Wow, that’s really simple!!
This is a major WP problem. These exciting features are not easily accessible by the docs, if at all.
Thanks.
March 28th, 2006 at 9:47 am
great work… however instead of writing the whole special post, you can put just that part of the code for the special post that differs from the mundane post. that way there will be less code to write.
March 29th, 2006 at 11:28 am
Well, yes, that would be less code. However, the example I showed is a very generalized one, and only serves to show what is being done, so that those using it will have a better understanding of it.
March 31st, 2006 at 7:15 pm
Any idea how to make this ‘variable’ (like odd-even in EE) ?
Sorry I am a php-n00b.
April 1st, 2006 at 1:56 am
if you want to have odd posts be different, using the loop in the post, you can use something like:
<?php if ($i % 2 != 0) : ?>
odd post
<?php else: ?>
even post
<?php endif; ?>
So all together that would be:
<?php $i = 1; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ($i % 2 != 0) : ?>
odd posts stuff here
<?php else: ?>
even posts posts stuff here
<?php endif; ?>
<?php $i=$i 1; ?>
<?php endwhile; ?>
<?php else : ?>
<p>No post found.</p>
<?php endif; ?>
April 5th, 2006 at 3:09 am
Thanks for that Matt. Very nice to know that. Didn’t use it (yet), but will definitely in the future.
April 10th, 2006 at 2:04 am
Hi all,
(Something totally different) What code do you use to format the box with your pingbacks. I am new to wordpress and I did not get the concept yet. Did you use javascript for the show hide thing?
Could you post (or email) me the code? Please?
Thanks, dl33
April 10th, 2006 at 2:15 am
*dl33* : The pingback box is coded by my great “Chris J Davis”:http://chrisjdavis.org
If you want to play around with it, download his wonderful theme So fresh “from here”:http://www.chrisjdavis.org/2005/12/31/a-couple-of-gifts-for-you/ and you’ll find the code already embedded within it.
If, however, you still need help, ask.
April 11th, 2006 at 1:08 am
Sweeet, thanks….
June 23rd, 2007 at 7:38 am
Although it was a very very old post; i want to thank you so much cause it helped me a lot… keep up good work