Currently, I Just Added This Script on Functions.php
// DAFUNDA CSS Class Based on Category
function category_id_class($classes) {
global $post;
foreach((get_the_category($post->ID)) as $category)
$classes[] = $category->category_nicename;
return $classes;
}
add_filter(‘post_class’, ‘category_id_class’);
add_filter(‘body_class’, ‘category_id_class’);
My Problem is, I Have multiple Category in One Post. Sometimes, the CSS will be Mixed. My Questions is how to make this functions only work with Primary/Parent Category?
EXAMPLE
https://dafunda.com
using Different color in Each Post based on Category
https://dafunda.com/tekno/
https://dafunda.com/tekno/cara-merawat-baterai-tahan-lama/
The Problem is Here
https://dafunda.com/gokil/
https://dafunda.com/gokil/inilah-petunjuk-toilet-lucu-dan-keren/
HI
I assume that you want to add the name of primary category as a class on body. If so try this code in functions.php:
function category_id_class($classes) {
global $post;
$td_post_settings = get_post_meta($post->ID, 'td_post_theme_settings', true);
if (!empty($td_post_settings['td_primary_cat'])) {
$primary_category = get_category($td_post_settings['td_primary_cat']);
}
$category = get_the_category($post->ID);
if(!empty($primary_category)){
$classes[] = $primary_category->category_nicename;
}
else {
foreach((get_the_category($post->ID)) as $category) {
$classes[] = $category->category_nicename;
}
}
return $classes;
}
add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');
If you have a primary category set for one post its name will be added on body classes. If the post does not have a primary category on body will be added the names of categories assigned for it.
Hope this helps.
Thanks!
I Have A Different Problem Now, Dafunda.com Mobile Theme Didn’t Work Correctly. Sometimes, Mobile theme Plugin is active. the other time, it’s look like deactivated.
Hello,
We investigated this issue a bit as we have received reports of it since version 4.6 of wordpress. It seems there is a compatibility issue regarding wp super cache and the new version of wordpress. The same thing happens on a default theme and the jetpack mobile theme for example so it;s not theme related nor a problem with the mobile theme. We will have to wait until wp super cache releases an update to fix any incompatibility issues.
Thank you!