• Resolved bhammondCVL

    (@bhammondcvl)


    I have two categories on my site that are necessary to keep, but should not be displayed on the site’s homepage. How do I modify either the child theme’s functions.php or template-knowledgebase.php to exclude those two categories?

    I have already tried several plugins supposed to exclude categories from the homepage (such as Advanced Category Excluder, Ultimate Category Excluder, and WP Exclude From Homepage) and they do not work.

    I have also tried code snippets in functions.php, such as the one below, that several sites recommend to exclude categories from the homepage. This code also does not work.

    function exclude_category_home( $query ) {
    if ( $query->is_front ) {
    $query->set( 'cat', '-1, -58' ); }
    return $query;}
    add_filter( 'pre_get_posts', 'exclude_category_home' );

    Any suggestions would be appreciated.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter bhammondCVL

    (@bhammondcvl)

    With some help from a colleague, I figured this out. The code snippet above works to exclude posts from the_loop, but the knowledgebase homepage does not use the_loop to build its list of categories.

    But you can alter the template-knowledgebase.php to exclude any category ID you’d like.

    Below is how I’ve added the ‘exclude’ argument to the get_categories function to exclude categories 1 and 58.

    $main_categories = get_categories( array(
    	'taxonomy' => 'category',
    	'parent' => 0,
    	'hide_empty' => 0,
    	'exclude' => '1,58'
    ) );
    Thread Starter bhammondCVL

    (@bhammondcvl)

    Oops; forgot to mark thread ‘resolved.’

    Thanks for the share !

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to exclude categories from the homepage?’ is closed to new replies.