WordPress.org

Ready to get started?Download WordPress

Forums

How to hide posts from 2 or 3 categories from index page (7 posts)

  1. cincen
    Member
    Posted 9 years ago #

    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

  2. rwatkins
    Member
    Posted 9 years ago #

    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

  3. iand
    Member
    Posted 9 years ago #

    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.

  4. rwatkins
    Member
    Posted 9 years ago #

    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 :).

  5. thepete
    Member
    Posted 8 years ago #

    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?

  6. thepete
    Member
    Posted 8 years ago #

    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?

  7. kindpastor
    Member
    Posted 8 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.