Home › Forums › General Issues › Add category name to body classes
Hi,
It seems that the dropdowm menu I choose the categories from when I edit an article as been implemented with ACF.
It would explain why the solution I saw everywhere to add the category name in the body classes does not work for me. It does nothing.
I am talking about this one there to put in function.php :
add_filter('body_class','add_category_to_single');
function add_category_to_single($classes) {
if (is_single() ) {
global $post;
foreach((get_the_category($post->ID)) as $category) {
// add category slug to the $classes array
$classes[] = $category->category_nicename;
}
}
// return the $classes array
return $classes;
}
So I guess there is a way to achieve that with a different code assuming this category dropdown was implemented with ACF.
I’d be really happy if you can help me for this first ever question I ask here ?
I thought I should add more details.
Here are my investigations :
There is a screen capture of my post edition page attached.
I found a json file in my theme that seems to deal with categories in an ACF-jon folder :
{
"key": "group_5ac5d1e87ab94",
"title": "Categorie",
"fields": [
{
"key": "field_5ad704be7333f",
"label": "Afficher la catégorie courante",
"name": "afficher_les_articles_dans_la_page_categorie",
"type": "true_false",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"message": "",
"default_value": 1,
"ui": 1,
"ui_on_text": "",
"ui_off_text": ""
}
],
"location": [
[
{
"param": "taxonomy",
"operator": "==",
"value": "categorie"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": "",
"modified": 1530175641
}
And finally that’s what I tried to add in function.php with no success :
add_filter('body_class', 'add_acf_body_class');
function add_acf_body_class($class) {
$value = get_field('afficher_les_articles_dans_la_page_categorie', false, false);
$class[] = $value;
return $class;
}
The topic ‘Add category name to body classes’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.