-
AuthorPosts
-
April 11, 2017 at 10:15 am #775911
Hi, Gianluca from Italy.
Need to hide name categories from posts everywhere in my site mfm.it/demo for example category TOP. Simply hide the category, non the post belongs to it.
I found a thread dated 2012 (https://kriesi.at/support/topic/hide-a-category-from-blog/) but i thin now it’s obsolete. Can u help me please? I have alrealdy a child theme. Thanks in adavance, GianlucaApril 11, 2017 at 10:44 am #775923Hey sitibus,
This answer should help you: http://wordpress.stackexchange.com/questions/181182/hide-a-certain-category-name-without-removing-it
Best regards,
John TorvikApril 11, 2017 at 4:39 pm #776168Hi, I’ve read and understood that i have to put in my functions.php thise code is it right?
add_filter(‘get_the_terms’, ‘hide_categories_terms’, 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){// define which category IDs you want to hide
$excludeIDs = array(6);// get all the terms
$exclude = array();
foreach ($excludeIDs as $id) {
$exclude[] = get_term_by(‘id’, $id, ‘category’);
}// filter the categories
if (!is_admin()) {
foreach($terms as $key => $term){
if($term->taxonomy == “category”){
foreach ($exclude as $exKey => $exTerm) {
if($term->term_id == $exTerm->term_id) unset($terms[$key]);
}
}
}
}return $terms;
}But where do I have to set the IDs of the categories (such as 34, 45) ?
Thanks again Gianluca
April 12, 2017 at 5:43 am #776451Hi,
That should be this line:
$excludeIDs = array(6);
So in your case it would be something like this:
$excludeIDs = array(34, 45);
Best regards,
RikardApril 17, 2017 at 10:43 pm #778984Sorry again but it doesn’t work as you can see at http://www.mfm.it/demo/
I added the code:add_filter(‘get_the_terms’, ‘hide_categories_terms’, 10, 3);
function hide_categories_terms($terms, $post_id, $taxonomy){// define which category IDs you want to hide
$excludeIDs = array(6960, 7097);// get all the terms
$exclude = array();
foreach ($excludeIDs as $id) {
$exclude[] = get_term_by(‘id’, $id, ‘category’);
}// filter the categories
if (!is_admin()) {
foreach($terms as $key => $term){
if($term->taxonomy == “category”){
foreach ($exclude as $exKey => $exTerm) {
if($term->term_id == $exTerm->term_id) unset($terms[$key]);
}
}
}
}return $terms;
}where 6960 and 7097 are the IDs of categories TOP & DISCOVER but as you can see those taxonomies are still visible.
Please help me. Thanks Gianluca
April 19, 2017 at 7:31 am #779823Hi,
What is the blog layout that you’re using? Please replace the filter with the following.
add_filter('get_the_terms', 'hide_categories_terms', 10, 3); function hide_categories_terms($terms, $post_id, $taxonomy){ $exclude = array(6, 2, 3); if (!is_admin()) { foreach($terms as $key => $term){ if(in_array($term->term_id, $exclude)) unset($terms[$key]); } } return $terms; }
In the $exclude variable, add the id of the term that you want to exclude.
Best regards,
IsmaelApril 19, 2017 at 12:40 pm #779927Hi Ismael
great job, it works perfectly.
Wonderful :-)))April 19, 2017 at 1:09 pm #779939Hi Gianluca,
We’re glad the code worked for you! Please let us know here in the forums if you need help with anything else.Cheers,
Sarah -
AuthorPosts
- The topic ‘Hide specific categories from meta’ is closed to new replies.