WordPress.org

Forums

[resolved] Excluding category on Showcase template in twenty eleven theme (8 posts)

  1. tj000012
    Member
    Posted 3 years ago #

    Hi there,

    Basically my need is to exclude a category of posts from showing in the showcase template of the fantastic Twenty Eleven theme. I'm not great at PHP, however can sometimes get things to work with not too much trouble. However this one is baffling me...

    I've tried an 'if is page' statement, exclude this category id, before the loops starts. however nothing seems to be hiding this particular category!

    Any help would be... just.... so great.
    Thanks

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    // SORT POSTS
    function voodoo_sort(&$query) {
            if (is_front_page() ){
            $query->set('cat', '-33');
         }
    }
    add_action('pre_get_posts', 'voodoo_sort');

    Are you running a child theme? If not, you need to be. Never edit the parent theme (twentyeleven) check http://vudu.me/child for more on that

    Anyway, I think the above would exclude a category from the homepage (added to functions.php)

    is_front_page may need to be swapped with is_home ..... not sure

    And of course, where I have -33, you put your cat ID, keep the minus sign, that excludes cats

  3. tj000012
    Member
    Posted 3 years ago #

    Thank you for your quick response.
    Very much appreciated.

    I would appreciate if there was a solution that didn't involve any reference to dark magical powers. WordPress has such a function? That stuff is deadly dangerous! Yikes

    Something like this, should work right?

    function exclude_category($query) {
    if ( $query->is_page('blog') ) {
    $query->set('cat', '-22');
    }
    return $query;
    }
    add_filter('pre_get_posts', 'exclude_category');

    ===
    No Child theme this time round. However I agree I could have used a child theme, as is probably better practice.

    I have tried adding some similar functions, however it seems to be not working for some reason at all. Even targeting the right category id.

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 3 years ago #

    If you don't use a child theme, your changes WILL be lost at some point. As twentyeleven is the core/default theme, if it gets upgraded, it will be overwritten on your install and your changes will be lost.

    Also, twentyeleven is needed in an unaltered state as a fallback if something goes wrong on your site.

    Also, the voodoo_ prefix I use on my functions is simply to avoid collisions. You can rename a function as you like.

    There is a chance that WP iteself, or some plugin would use a name like exclude_category, however I doubt you'll find voodoo_sort

    Name it whatever, the idea is to name functions with some sort of prefix to avoid the possibility of a conflict

  5. tj000012
    Member
    Posted 3 years ago #

    Gotcha, thanks for clearing that up.

    Thanks for the help. I might go back to the drawing board!

    Not necessarily re not using a child theme. I'd just rename it and use it as a starting point and then customise/ customize to the max :)

    And likewise if you use a child theme based off twentyeleven, it may change at some point if you update wordpress. Which is why I generally don't bother with them. Pros and Cons. However they are valid and that's for another discussion!

    Cheers

  6. tj000012
    Member
    Posted 3 years ago #

    Function; customised. worked great. Cheers
    This one is solved.

  7. berelson@hotmail.com
    Member
    Posted 2 years ago #

    I'm using twenty eleven and a child theme. I've added this at the beginning of my functions page (in child) with cat -40 (which is the cat id i'm trying to exclude). The syntax 'home_page' broke my display on all pages. Using 'front_page' didn't break site but didn't work with current config. I'm using a static homepage but the post said it should work on post pages and it didn't on my news page which uses a showcase (dynamic post) template.

    // SORT POSTS
    function voodoo_sort(&$query) {
            if (is_home_page() ){
            $query->set('cat', '-40');
         }
    }
    add_action('pre_get_posts', 'voodoo_sort');
    
    //**started a new series of functions which work fine from here...

    Also, am i to use the 'voodoo_sort' (or whatever we call it) function in some other file? e.g. page.php?? single.php?? etc.

  8. berelson@hotmail.com
    Member
    Posted 2 years ago #

    delete this... added to prior comment.

Topic Closed

This topic has been closed to new replies.

About this Topic