Created
June 16, 2023 12:16
-
-
Save elias1435/d1f86768b8f8ac6104c1f0f02dfac97d to your computer and use it in GitHub Desktop.
How to Add Category Id to WordPress Body and Post Class This snippet is very handy if you wish to add category ID to WordPress body and post class for ex. targeting CSS styles and such.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function category_id_class($classes) { | |
global $post; | |
foreach((get_the_category($post->ID)) as $category) | |
$classes [] = 'cat-' . $category->cat_ID . '-id'; | |
//if you want to add category slug replace below code | |
//$classes [] = 'cat-' . $category->slug; | |
return $classes; | |
} | |
add_filter('post_class', 'category_id_class'); | |
add_filter('body_class', 'category_id_class'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment