How to: Display only one sticky post on your homepage

Do you use WordPress as a CMS instead of a blog? If yes, you’ll probably be interested to know how you can display a single post on your blog homepage.

Open your index.php file, and replace your current loop by this one (replacing the stickies category name by the category of your choice) by :

<?php if (have_posts()) :
$my_query = new WP_Query('category_name=stickies&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post(); ?>

That's all! Only the latest post is displayed on your homepage.

5 Responses

Nov 28 2008 09:47

Actually wouldn’t this require you to have a category named stickies … this can be accomplished for any category by changing the stickies bit in the code to the category name that you choose.

Also, a better way of doing this is probably to just create a page as opposed to a post and use that as the main landing page for your blog.

Nov 28 2008 10:13

@Sharninder: That what I meant with the “stickies” category, I forgot to explain it, sorry for that.
Exactly, the same thing can be accomplished by setting up a static page as the homepage. The only difference is that with that recipe, you’ll not have to update the page, and can display old posts on a archive page, for exemple.

Dec 06 2008 07:17

Hmm,I almost thought this was in relation to the Sticky Post feature of WordPress, but it’s not part of it, is it?
Besides using the category name we can also choose to use cat=1,2, so on and so fort, right?

Dec 06 2008 10:01

@Kevin Paquet: exactly, you can do it like this :)

Dec 10 2008 15:15

Thanks for the clarification, I always get confused about this, because a lot of themes are instructing to use specific category names, I really wondered why, because I’m personally used to use the Category I.D’s instead.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required