WordPress.org

Ready to get started?Download WordPress

Forums

Show one category on index.php (49 posts)

  1. poper
    Member
    Posted 9 years ago #

    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

  2. Mark (podz)
    Support Maven
    Posted 9 years ago #

    What do you mean by 'can't get it to work ' ?
    Errors ? More than one cat showing ?

  3. poper
    Member
    Posted 9 years ago #

    I mean that nothing happens, all categories are shown (as usual). No error messages.

  4. poper
    Member
    Posted 9 years ago #

    nobody knows?

  5. divrom
    Member
    Posted 9 years ago #

    Try just this:
    <?php
    $blog = 1;
    $cat="1";
    /* Don't remove this line. */
    require('./wp-blog-header.php');
    ?>
    If that doesn't work, you could try mootkitty's show categories plug-in:
    http://mookitty.co.uk/devblog/category/show-categories/

  6. poper
    Member
    Posted 9 years ago #

    I've tried show-categories and it just works really fine. Thanks a lot!

  7. Kerim
    Member
    Posted 9 years ago #

    It seems that the "show one category" hack (from the wiki) is broken in the latest build of 1.3.
    http://www.wiki.wordpress.org/ShowOneCategory
    Anyone know how to fix it? I really need this implementation, because as far as I can tell the "show-categories" plugin doesn't seem to have the same functionality. What I want is the ability to have index.php show the default category only when no other category is selected, not to permanently exclude any categories.
    Thanks!

  8. joand
    Member
    Posted 9 years ago #

    It is broken in 1.2 also.

  9. TechGnome
    Moderator
    Posted 9 years ago #

    Best way to do this is like so:

    if (!isset($cat)) {
    $cat = "X";
    }

    Where X is the category you want to show. If you want to show more than on then it's "X,Y, Z,...." If you want to exclude a category - and at the moment, at least in 1.2 - you can only exclude one category, it's like this: $cat = "-X"; Again, where X is the category you want to exclude.
    TG

  10. HalAtkins
    Member
    Posted 9 years ago #

    TG -
    Where do you put that code? In the loop?
    Thanks,
    Hal Atkins

  11. TechGnome
    Moderator
    Posted 9 years ago #

    no no no no. IMMEDIATLY after the $blog=1; which should be the second line in the index.php file. Like so:

    <?php
    $blog = 1;
    if (!isset($cat)) {
    $cat = "X";
    }
    /* Don't remove this line. */
    require('./wp-blog-header.php');

    TG

  12. HalAtkins
    Member
    Posted 9 years ago #

    Thanks, TG-
    Works well, and appreciate your help. I understand, setting the $cat variable - out of interest, what is the $blog variable? Is it for multiple blogs on the site?
    Hal

  13. Kerim
    Member
    Posted 9 years ago #

    That works for me!

  14. TechGnome
    Moderator
    Posted 9 years ago #

    Not sure.... it's something that has been there since I've been using WP. Don't really know what it's for. If it isn't there, I wouldn't worry about it.
    TG

  15. Joey_aka_Pepe
    Member
    Posted 9 years ago #

    I had the same dilemma, but instead of searching the forums here (as I should have!), I went and created an index.html which just redirects to index.php?cat=1. So far, it works, but is there any downside to using such a "dirty trick" to get only one category shown?
    Cheers, Joey (aka Pepe)

  16. TechGnome
    Moderator
    Posted 9 years ago #

    some of the links generated by WP point right to the index.php file, which would skip your redirect. Also, it is possible to disable redirects in a browser (I've known people to do this) and so they would never get to your file. Also, I don't know how you feel about it, but you may also run into problems with search engines indexing the site properly -some people care about such things, others don't, I meerly point it out.
    TG

  17. Anonymous
    Unregistered
    Posted 9 years ago #

    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.

  18. Joey_aka_Pepe
    Member
    Posted 9 years ago #

    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!

  19. Root
    Member
    Posted 9 years ago #

    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 ?

  20. AaronLawrence
    Member
    Posted 9 years ago #

    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.

  21. davejenbarnes
    Member
    Posted 9 years ago #

    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!

  22. Ryan Boren
    WordPress Dev
    Posted 9 years ago #

    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');
    ?>

  23. Jack
    Member
    Posted 9 years ago #

    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?

  24. TechGnome
    Moderator
    Posted 9 years ago #

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

  25. miguelvb
    Member
    Posted 9 years ago #

    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

  26. nolageek
    Member
    Posted 9 years ago #

    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.

  27. Kerim
    Member
    Posted 9 years ago #

  28. asknight
    Member
    Posted 9 years ago #

    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;

  29. dss
    Member
    Posted 9 years ago #

    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?

  30. Roar
    Member
    Posted 9 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.