Hide category from front page but show on slider

Viewing 11 posts - 1 through 11 (of 11 total)
  • #15365
    gunbunnysoulja
    Participant

    Could you possibly help me out with hiding a particular category from the front page, except for those on the slider via sticky?

    I am currently using $query->set( ‘post__not_in’, get_option( ‘sticky_posts’ ) ); to hide duplicates, which works great for that purpose.

    #15371
    Daniel Tara
    Keymaster

    you can use the category__not_in parameter just as you use post__not_in.

    #15373
    gunbunnysoulja
    Participant

    Daniel, thanks for the reply. Could you possible elaborate a little more since I’m not really good with code. Do I modify that part of the existing code, or write this code in addition to what is already there.

    The category that I want to take off the front page but keep on the slider is category 29.

    Thanks so much!

    #15376
    Daniel Tara
    Keymaster

    You need to make this call:

    $query->set( ‘category__not_in’, array( 29 ) );
    #15378
    gunbunnysoulja
    Participant

    Daniel,

    Thanks again so much. Where do I insert that? Can I keep the existing function code of $query->set( ‘post__not_in’, get_option( ‘sticky_posts’ ) );

    I tried that code before but it keeps breaking, so I’m assuming I’m putting it in the wrong area, or its conflicting somewhere.

    #15379
    gunbunnysoulja
    Participant

    To clarify, do I need to make an entirely new function call?

    This is what I have:

    function esplanade_ignore_sticky_posts( $query ) {
    global $wp_the_query;
    if( ( $wp_the_query === $query ) && $query->is_home() && esplanade_get_option( ‘slider’ ) )
    $query->set( ‘post__not_in’, get_option( ‘sticky_posts’ ) );
    }
    endif;

    #15380
    gunbunnysoulja
    Participant

    Trying this:

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set(‘category__not_in’, ’29’);
    }
    return $query;
    }
    add_filter(‘pre_get_posts’, ‘exclude_category’);

    #15381
    gunbunnysoulja
    Participant

    My code above didn’t work, as it hid from the slider too, as well as somehow now showing categories already being hidden by a plugin.

    #15382
    gunbunnysoulja
    Participant

    I should note, I did include array on the actual code, so you can disregard me missing it above.

    #15414
    gunbunnysoulja
    Participant

    Almost every code I do breaks the site. I must be doing the function structure wrong.

    I have also tried:

    function exclude_category($query) {
    if ( $query->is_home ) {
    $query->set( ‘category__not_in’, array( 29 ) );
    }
    return $query;
    }
    add_filter(‘pre_get_posts’, ‘exclude_category’);
    remove_filter(‘pre_get_posts’, ‘exclude_category’);

    as well as other variations. Any help would be greatly appreciated!

    #15418
    gunbunnysoulja
    Participant

    Finally figured it out! I wrapped this into the existing function:

    if( ( $wp_the_query === $query ) && $query->is_home() && esplanade_get_option( ‘slider’ ) )
    $query->set( ‘cat’, -29 );

    So the entire function looks like:

    function esplanade_ignore_sticky_posts( $query ) {
    global $wp_the_query;
    if( ( $wp_the_query === $query ) && $query->is_home() && esplanade_get_option( ‘slider’ ) )
    $query->set( ‘post__not_in’, get_option( ‘sticky_posts’ ) );
    if( ( $wp_the_query === $query ) && $query->is_home() && esplanade_get_option( ‘slider’ ) )
    $query->set( ‘cat’, -29 );
    }
    endif;

    add_action( ‘pre_get_posts’, ‘esplanade_ignore_sticky_posts’ );

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.