Site logo

Archived topic

hiding specific categories from meta

9 replies · Started by dale on April 11, 2018

Viewing posts 1–10 of 10

Hi! I have a couple categories that I only use for organization on the back end and don't want visible on the front end—meaning I'd like to hide them from the meta area that appears below each post. I tried this, where "feature" and "10" are standins for the category:

function the_category_filter($thelist,$separator=' ') {
if(!defined('WP_ADMIN')) {
//list the category names to exclude
$exclude = array('feature');
$cats = explode($separator,$thelist);
$newlist = array();
foreach($cats as $cat) {
$catname = trim(strip_tags($cat));
if(!in_array($catname,$exclude))
$newlist[] = $cat;
}
return implode($separator,$newlist);
} else
return $thelist;
}
add_filter('the_category','the_category_filter',10);`

pasted into Code Snippets and activated, but it didn't work. Thoughts?

Thanks for your response.

That plug-in hides actual posts. I am just looking to hide the name of the category from the meta that appears beneath each post.

For example, on page https://www.dalecameronlowry.com/submission-call-fairy-tales-about-summer-nights-pays-20-due-may-20/, I have two categories showing in the meta at the bottom of the page: "Calls for Submissions, For Writers"

let's say that I don't want readers clicking on "For writers" because it's only used for backend organization. So I want to keep the post in that category, but have the meta only read "Calls for Submissions"

Is that clearer?

Ah, gotcha.

Untested, but you could try this filter:

add_filter( 'get_terms', 'tu_remove_terms' );
function tu_remove_terms( $terms ) {
    unset( $terms['category_slug'] );

    return $terms;
}

Thanks. I added it like this to Code Snippets:

add_filter( 'get_terms', 'tu_remove_terms' );
function tu_remove_terms( $terms ) {
    unset( $terms ('for-writers') );

    return $terms;
}

But nothing happened. I looked in incognito and purged the site-side caches as well. Did I adapt it incorrectly?

That doesn't seem to work either. I'll keep looking for solutions.

Hmm, strange. It may be worth asking on stackoverflow.com. Someone may have done this before.

You could use tags instead of categories for the internal tags. Then just choose not to display tags on the frontend.

I neded the same and It works in my case with this code:

add_filter('get_the_terms', 'hide_categories_terms', 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){
	$exclude = array(IDcat1,IDcat2...);

    if (!is_admin()) {
        foreach($terms as $key => $term){
            if(in_array($term->term_id, $exclude)) unset($terms[$key]);
        }
    }
    return $terms;
}

Source

Hi Lorenzo,

Thank you for sharing it with us. :)

This archived topic is closed to new replies.