Dynamic Web Lab
Back to insights

Include category id in body_class() and post_class()

January 25, 20141 min read

Use this code on your themes functions.php to add current category id on body and post
[php]
function category_id_class($classes) {
global $post;
foreach ((get_the_category($post->ID)) as $category)
$classes[] = ‘cat-‘ . $category->cat_ID . ‘-id’;
return $classes;
}
add_filter(‘post_class’, ‘category_id_class’);
add_filter(‘body_class’, ‘category_id_class’);
[/php]

Writer

Dynamic Web Lab Editorial

We share how we design, engineer, and scale digital products across the GCC, Europe, and the US.

Share this article

Need help implementing this?

We turn these playbooks into shipped features. Let us scope your roadmap and support your team.

Start a project conversation
Include category id in body_class() and post_class() - Demo Dynamic Web Lab