CSS Class Based on Primary Category Ini Newspaper Theme

Posted in: Newspaper
Post count: 42

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/

Post count: 6535

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!

Post count: 42

Thanks Andrei L.
I’ll Try it now… 😀

Post count: 42

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.

Post count: 22421

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!

Post count: 42

Thanks Bogdan B.
I’ll Wait for it. 😀

Viewing 6 posts - 1 through 6 (of 6 total)
The forum ‘Newspaper’ is closed to new topics and replies.