Support » Fixing WordPress » Show one category on index.php

  • I know there’s a plugin “ShowOneCategory” that seems to do what I want, but I can’t get it to work. I think I’ve followed the directions properly. This is the code (+ ‘post paged’ set):
    <?php
    /* Don't remove this line. */
    /* index.php hacks */
    //Set index.php to only show one category on main page
    if ('/' == $_SERVER['REQUEST_URI'] || '/index.php' == $_SERVER['REQUEST_URI'])
    $cat = 1;
    /* --------------- */
    require('./wp-blog-header.php');
    ?>

    Is wrong?
    Is there another way to show just a specified category on my front page? Please help me, I’m really desperate.
    Thanks

Viewing 15 replies - 16 through 30 (of 48 total)
  • I’ve tried:
    <?php
    if (!isset($cat))
    $cat = 1;
    ?>
    And now all post of $cat=1 are displayed contrary to what I’ve configurated on wordpress “reading opitons” section.
    This may not be a problem to everyone but it is quite troubling for me.

    Thanks for the info, TG! I followed your advice and changed my index.php as per the instructions in this thread, so I no longer have the redirection.
    Felipe, I don’t quite understand what you’re trying to do… you’re trying to show only some cat=1 posts? Or you don’t want cat=1 posts to show up at all? In the first case, I’ve no idea what to do, in the second, just change the $cat=1 to the actual category you wish to show. If this has nothing to do with your problem, sorry!

    Techgnome thanks for the fix but I could not call my archives any longer. No matches for the criteria. I didnt want them anyway but I am curious ?

    The problem I found with the above hack/mod is that once you press “next page” to go to page 2 of the listings, it ceases to work. At least in my case it did. I was attempting to display all entries except those from a single category on the main listings. It would correct display messages on the main index.php page, but once you went to index.php?paged=2, the count was thrown off because it now factored in the messages that I did not want to be displayed.
    Fortunately a solution was easy. Instead of:
    if (‘/’ == $_SERVER[‘REQUEST_URI’] || ‘/index.php’ == $_SERVER[‘REQUEST_URI’])
    I used:
    if (‘/’ == $_SERVER[‘REQUEST_URI’] || ‘/index.php’ == $_SERVER[‘REQUEST_URI’] || strstr($_SERVER[‘REQUEST_URI’],’/index.php?paged=’))
    … and that appears to have solved all my troubles. This is particularly useful when using the pictpress plugin, as it creates extra messages for every picture it sticks in the gallery message. I put all the extra messages in a dummy category, and can now avoid them cluttering up the count on my displays.

    I added
    if (!isset($cat)) {
    $cat = “X”;
    }
    to the recommended location at the top of index.php and it works beautifully BUT it causes a big delay in almost all of my WordPress pages loading.
    There is only one post I am trying not to display and it is being updated every day (and I change the timestamp) but I just don’t want it to display other than a direct link to the page. If there is a faster way to do this and get my post #270 to NOT show up, I’d like to know… if not, this is [hopefully] a temporary solution and will only cause me to find a better solution quickly!

    Ryan Boren

    (@ryan)

    WordPress Dev

    If you’re using 1.3, you can try this plugin:
    <?php
    /*
    Plugin Name: Front Page Categories
    Version: 0.1
    Plugin URI: http://wordpress.org/#
    Description: Select categories to display on the front page.
    Author: Ryan Boren
    */

    function fpc_front_page_cats($query_string) {
    // Change this to the categories you want to show on the front page.
    $cats_to_show = '1 2 3';
    $query = new WP_Query();
    $query->parse_query($query_string);
    if ($query->is_home) {
    if (! empty($query_string)) {
    $query_string .= '&';
    }
    $query_string .= "cat=$cats_to_show";
    }
    return $query_string;
    }
    add_filter('query_string', 'fpc_front_page_cats');
    ?>

    rboren, could this code be changed into showing all the posts from the latest month that has posts? So if october hasn’t got any posts yet, show the full month of september?

    @davejenbarnes – see if this would be of any use to you:
    StaticEez Plugin
    TG

    miguelvb

    (@miguelvb)

    I’ve just tryed every thing that was proposed here, but no results….
    any advise?
    I have WP 1.3 alpha ….
    http://www.gatodante.com/wpress

    nolageek

    (@nolageek)

    I can’t believe that there’s no easy way to do this. Seems VERY basic if you ask me.
    Has anyone come up with a way to do this?? Geez.
    Running 1.3 here.

    luhmann

    (@luhmann)

    Ok, one thing you may be running in to is that wp-blog-header.php (see top of your index.php), when it sees a set $cat=#, makes the assumption that you also want all the children of the specified category too (if they exist of course), even if you don’t. This was my case, so I did the following:
    First, I wanted the index.php page to always insept with a certain set of posts so I ended up with this line after at the very beginning of index.php:: if (! isset($_GET[cat])) {$cat=6; $numberposts=9; $orderby="category"; $order="ASC"; $fetchkids=0;}
    Next, the following code changes were made to wordpres/wp-blog-header.php
    An aside thing to know is that 1.2.1 at least ‘allows’ only orderby= on 'author','date','category','title'
    I also added a consumable variable called ‘fetchkids’ at the end of $wpvarstoreset array at line 51. Then at line 264 the code changes to:

    if (! isset($fetchkids) || $fetchkids) {
    $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
    for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
    $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
    $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
    }
    }

    So, by setting $fetchkids to FALSE (0) above, and adding the code, I effectively broke out of the bit that would end up fetching all the children posts of my parent category.
    Yes, it’s a hack but no less elegant than the convention I see in place w/in wordpress/wp-blog-header.php;

    I am very curious about setting the category for the index page only.
    but if i add any mod to the header, won’t that effect the entire site?

    Can this plug-in be used on any other page besides index.php?
    for example a sub page, called books.php?

    line 256?
    hmm,
    where is that in the code?
    do i haev to count the lines?

Viewing 15 replies - 16 through 30 (of 48 total)
  • The topic ‘Show one category on index.php’ is closed to new replies.