Show Only 1 Wordpress Category In ‘index.php’

March 19th, 2006, Published in Wordpress, Web Development, 1,199 Views, Email This Email This

Wordpress

I wanted to just show my ‘website news’ category in the root of my Wordpress installation. But by default index.php shows the latest posts from all categories, this was not what I wanted. So how could I get what I want?

Step 1

I take a look in index.php. It displays:

<?php
/* Short and sweet */
define("WP_USE_THEMES", true);
require("./wp-blog-header.php");
?>

Step 2

After looking at this code I could see, clearly, there was no statement to restrict what category to show, which was what I wanted. So I checked in the Administration>Categories section in the Administration part of my site to find out the category ID of the category I wanted to just show on my index.php in my Wordpress installation root, the category ID was ‘2?. I changed index.php to the following and uploaded it to my webserver:

<?php
$blog = 1;
if (!isset($cat)) {
$cat = "2";
}
/* Don't remove this line. */
define("WP_USE_THEMES", true);
require("./wp-blog-header.php");
?>
Include Preset Text In Wordpress Posts >>

2 comments so far

  1. Gravatar 1 Trevor Turk
    5:44 pm - 9-18-2006

    Another alternative is to make a “home.php” file in your themes directory, which contains the contents of your “index.php” file. Then, you can just filter the category like so:

    If you don’t like the idea of all that duplication between “index.php” and “home.php”, you could just wrap things up in an IF statement checking if (is_home()).

  2. Gravatar 2 Trevor Turk
    5:47 pm - 9-18-2006

    Ahem… Like so:

    query_posts(’cat=1′); while ( have_posts() ) : the_post()

Leave a Reply

Your Details

Your Comment