Support » Fixing WordPress » How to hide posts from 2 or 3 categories from index page

  • I have been able to hide posts from one category from the index opage, but I would hide posts from more than one category.

    I am currently using this to hide category 8 from index in my theme index.php :

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php if ( !(in_category(‘8’))) { ?>

    HERE goes posts listings

    <?php } ?>
    <?php endwhile; ?>

    How would I hide for example category 7, 8 and 9 posts from home page?

    thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hey,

    You could use && (which means ‘and’) inside the if-statement:

    <?php if ( !(in_category('8')) && !(in_category('7')) && !(in_category('9'))) { ?>

    Therefore the post will only be displayed, if it’s not in category 8 and if it’s not in category 7 and if it’s not in category 9.

    Note that this will more than likely remove posts that are posted in multiple categories but one of the categories is one you want to filter.

    You could probably do this using a hook from the Plugin API but I have to admit I’m not too sure how.

    Rob

    What should work is using || for ‘or’. so
    <?php if ( !(in_category('8')) || !(in_category('7')) || !(in_category('9'))) { ?> but I haven’t had much luck with that so just repeat the <?php if ( !(in_category('8'))) { ?> statement for each category and add the appropriate number of closing }s.

    Hey,

    The reason the || doesn’t work is a little strange but it makes sense if you think about it.

    <?php if ( !(in_category('8')) || !(in_category('7')) || !(in_category('9'))) { ?>

    translates to: if it’s not in category 8 OR it’s not in category 7 OR it’s not in category 9.

    For an statement using OR to be true, you need any part of it to be true:

    True || Anything = True
    False || False = False

    And so with the statement you gave, if it’s in, say, category 8, then you would end up with the following statement:

    if ( false || true || true) which will always be true. (because in_category(‘8’) will be true, (which reversed is false) in_category(‘7’) will be false (which reversed is true)). And you only need one true for the whole thing to be true.

    What you actually want is:

    <?php if ( ! (in_category('8') || in_category('7') || in_category('9') )) { ?>

    So if it’s in either of those categories, the expression will evaluate to true, which negated is false.

    Rob

    ****

    (DeMorgan’s Theorem states that NOT(a OR b OR c) is the same as (NOT a) AND (NOT b) AND (NOT c) which is how you can have the statement using ORs and the statement using ANDs doing exactly the same). My brain is all mushy but I think that’s right :).

    Well, none of the examples work for me works for me. I get this error:

    Parse error: parse error in /home2/www/thepete/index.php on line 288

    Might it be something with 1.5.1.1?

    I’ve found my solution using another plugin–but I’m still interested in finding a way to hide posts from the list created when using the <?php get_archives('postbypost', '5'); ?> tag. Any ideas?

    what plugin would that be? I would like to do the same thing

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to hide posts from 2 or 3 categories from index page’ is closed to new replies.