WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Exclude multiple categories from home page (16 posts)

  1. tylerhauser
    Member
    Posted 7 years ago #

    I know this topic has been covered quite a bit here but I've never found an answer that worked for me. I just want to exclude multiple categories from my main page. Seems simple but it hasn't been.

    The closest I've come to finding a solution was a plugin called "Front Page Categories". This does exactly what I want it to do except it shows posts that are in more than one category, more than one time. Is there a way to stop this from happening?

    My site: TwoWordHeap.com

    Thanks!

  2. MichaelH
    Member
    Posted 7 years ago #

  3. Chris_K
    Member
    Posted 7 years ago #

  4. tylerhauser
    Member
    Posted 7 years ago #

    Thanks MichaelH, but the codex only seems to offer the exclusion of single categories (unless I'm reading it wrong, of course) and the ‘Category Visibility’ plugin does the same thing as the 'Front Page Categories' plugin I'm currently using. They seem to work great except they show duplicate posts if they fall into 2 or more categories. I can't find a way around this. Any ideas?

  5. tylerhauser
    Member
    Posted 7 years ago #

    That did it!!! Thank you so much HandySolo! The Ultimate Category Excluder works like a dream. Man, I love this support forum.

  6. gspark
    Member
    Posted 6 years ago #

    I compare this support forum to a huge dumpster, you have to jump in and get dirty and you may find something eatable or useable but they are very few, i always feel dirty after digging through the forum, very time consuming,

  7. jrothe
    Member
    Posted 6 years ago #

    Ultimate Category Excluder plugin is a little buggy for WordPress 2.2 and 2.3. Seems to exclude category posts from the homepage, but it also messes up what your links in the sidebar return.

    Seems like it was developed perfectly for up to version 2.1...might take some caution if using anything earlier.

  8. blogsnapper
    Member
    Posted 6 years ago #

    Another way of excluding more than one category from the home page is to add a small piece of code in your template (index.php etc).

    Look for (should be somewhere between line 1-20):
    <div class="post" id="post-<?php the_ID(); ?>">
    Insert this directly above:
    <?php if ((in_category(1) || in_category(2)) && is_home() ) continue; ?>

    Explanation: The code in this example looks for posts from categories with id 1 and 2. If they exist and the visitor are on the home page (is_home), they are hidden.
    If the visitor are viewing another category page than home, the posts will be visible.

    Exclude one category only?
    If you only want to exclude/hide one single category from the home page, use this code instead:
    <?php if (in_category('1') && is_home() ) continue; ?>

    Remember!
    Hiding a category won't make it invisible to the system. If you have specified to show 10 posts in a category page and 3 of the posts are hidden (as described above), the total amount of visible posts will be 7. The system will not add three new posts from the archive since it consider it allready shows 10 (as defined in dashboard). More advanced code is needed to accomplish this.

    This solution has been tested in WordPress 2.5

    Enjoy,
    //Blogsnapper

  9. football-iraq.org
    Member
    Posted 6 years ago #

    tylerhauser

    what plugin are you using on your website to display the subcategories when you point or mouse on the main category?

    can you please help me with the name?

    Thanks

  10. corpodibacco
    Member
    Posted 6 years ago #

    It is a big problem with this method.

    * if you display say 10 posts in home page
    * and all but one of them are of excluded categories

    you will find yourself with only 1 post in home page!!!

    => the exclusion should happen at sql level.

    I could write the SQL and put it in a function, but to what action should I hook it?

  11. pshero
    Member
    Posted 6 years ago #

    Hey Gang, after a relentless search I finally found a solution that both excludes 1 or more categories from the homepage but keeps the post count per page correct.

    Add the following code anywhere in your theme's functions.php file. Add extra categories with a comma and a dash ('-13,-5,-2'). You can also use this same code to exclude categories from your feed by changing the is_home tag to is_feed. Enjoy.

    <?php
     ('-14,-12,-1')
    function myFilter($query) {
    	if ($query->is_home) {
    		$query->set('cat','-13');
    	}
    
    return $query;
    }
    add_filter('pre_get_posts','myFilter');
    ?>
  12. Drew Baker
    Member
    Posted 6 years ago #

    Hey PSHERO,

    Using your code I get the following error (I have WP2.5):

    Parse error: syntax error, unexpected T_FUNCTION in /MY URL/functions.php on line 3

    Any ideas?

  13. pshero
    Member
    Posted 6 years ago #

    dbaker, It took me a minute but I just realized that for some reason the forum removed the commented area of the code I posted but left the ('-14,-12,-1') line intact. If you remove that line everything will be fine. It should look like this: (I removed my code comments so it will print properly here.)

    <?php
    function myFilter($query) {
    	if ($query->is_home) {
    		$query->set('cat','-13');
    	}
    return $query;
    }
    add_filter('pre_get_posts','myFilter');
    ?>
  14. corpodibacco
    Member
    Posted 6 years ago #

    Hey PSHERO, I had come to the same solution, but it doesn't work for me. It returns the posts in REVERSED order (earlier first). I can't figure out why.
    I tried to do $query->set('order','DESC'); but somehow that was ignored...

    A different method of exclusion is detailed on this codex page: http://codex.wordpress.org/Custom_Queries
    but incredibly that method gives the same reverse results to me.

  15. corpodibacco
    Member
    Posted 6 years ago #

    I figured that the reversed posts are caused by a bug with mysql 5.0.51.

  16. rat86
    Member
    Posted 6 years ago #

    italian tutorial to solve this problem of MYSQL bug:
    http://rat86.netsons.org/blog/?p=406

    http://rat86.netsons.org/blog/?p=416

Topic Closed

This topic has been closed to new replies.

About this Topic