WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Hiding default category (6 posts)

  1. bobbykjack
    Member
    Posted 3 years ago #

    Since a default category is forced, and usually doesn't make sense to display, I think it should be made easier to exclude it from functions that fetch categories. I've done this on our installation by hacking get_the_category_list() to continue the foreach loop(s) under this condition:

    if ($category->term_id == get_option('default_category'))

    which works nicely, but is a hack that will be removed on the next upgrade.

    Is there momentum for getting this added in as an option somewhere?

  2. esmi
    Theme Diva & Forum Moderator
    Posted 3 years ago #

    Why not just remove any reference to categories in your theme?

  3. bobbykjack
    Member
    Posted 3 years ago #

    Because I still want to display every other category, just not the default one.

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    It really depends what level of exclusion you want, if you want to totally hide it from everywhere, admin and front side you can exclude using a filter on list_terms_exclusions.

    If you want to just hide it front side, this might suit you.

    add_filter( 'list_terms_exclusions', 'global_cat_exclude' );
    function global_cat_exclude( $terms ) {
    	if( !is_admin() )
    		$terms =  "AND t.term_id != " . get_option( 'default_category' ) . " ";
    	return $terms;
    }

    It should exclude the default category from any category function that utilises get_terms(), which at the very least the default widgets do.

    You could make the above more selective, but you'll have to clarify how excluded you want it to be.

    NOTE: The above example also prevents that category's archive page from working to (that might be a desired effect, maybe not).

    If you want the archive to still work, just modify the conditional to read.

    if( !is_admin() && !is_category() )

    Hope that helps..

    One last note: If that's your actual version to the right, the code might not work (code was tested on 3.0, should be good for 2.9.2 also).

  5. bobbykjack
    Member
    Posted 3 years ago #

    Those are great, detailed suggestions, t31os_ - thanks very much. I guess I want to exclude it from all front-end display as an absolute minimum, but probably no admin display - it's useful to see which posts need to be 'fixed', after all - so !is_admin() looks like the way to go.

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    It should do the job for excluding across various category functions, i had a quick dig into the category functions, and it looks like most if not all of the category functions have now been switched over to get_terms and get_term where appropriate, so the filter should carry across into the most common category functions, assuming you're not back on 2.6.2 as the thread suggests.

    If you have any problems, post back... ;)

Topic Closed

This topic has been closed to new replies.

About this Topic