This code automatically adds the<body>tag on the category page and article page by judging the WordPress category ID“ cat-6-id ”For example:
< body class =" archive category category-wordpress category-6 logged-in custom-background cat-6-id ">
The number is the ID number of the category, provided that the theme template must use the function:
- <body <? php body_class(); ?>>
With this very convenient function, you can define different styles for different classifications.
WordPress
Add the following code to the functions.php file of your current theme:
- 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');
Application examples:
For example: HotNewspro Theme , hide the hot articles (the top four pictures) on the classified archive and article pages with the classification ID of 6, and add the following in the theme style file style:
- .cat-6-id, #top _hot {
- display : none ;
- }
It can be used to define the text size and background color at any other location different from other classifications.
Source of original code: http://www.trickspanda.com/2014/04/add-category-id-body-post-class-wordpress/
Most of the articles on this site are original and used for personal learning records, which may be helpful to you, for reference only!