ss_blog_claim=f80c49302de8a7ebba6fad691a0ad6df Displaying Only One Post On A Sites Index In Wordpress - Take More Risks
Take More Risks

Displaying Only One Post On A Sites Index In Wordpress

by Matt on , under Web Development

By default Wordpress lists your 15 most recent posts on the index page of your site. Sometimes it’s necessary to display only post, as was the case with my Gothic Fairies website. I’m sure there are probably more elegant solutions to this problem out there but regardless of this I thought I’d share my makeshift solution anyway.

I embedded the following code into the header section of the template just before the opening of the loop.

if($_SERVER["REQUEST_URI"] == "/" || $_SERVER["REQUEST_URI"] == "/index.php")
query_posts('p=3');

?>

In this example, the code tests to see if the current page is the index by checking the REQUEST_URI element of the $_SERVER array. If this variable equals either “/” or “index.php” the query_posts function is called which in turn selects a certain post to be displayed with the parameter referring to the specific post ID (which in this case is 3).

With a bit of luck – the post you just referred to will be the only one which appears on the front page of your website!

2 comments for this entry:
  1. thedark

    Wordpress have an option in the admin panel to limit the posts shown on the index page.

  2. Matt

    Ah cool.

    Is it possible to limit the number of posts for the index page alone or does that value apply for all of your categories? Say if I set it to 6 will 6 posts be shown on my index page and another 6 on say the Web Development category?

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...