WordPress怎么按分类ID为页面添加相应的class?

2022年11月3日 发表评论
免费、便宜/高性价比 服务器汇总(已更新):点击这里了解

WordPress怎么按分类ID为页面添加相应的class?

WordPress怎么按分类ID为页面添加相应的class?这段代码通过判断WordPress分类ID,自动在分类页面和文章页面的 标签中添加“cat-6-id”例如:

  1. <body class=”archive category category-wordpress category-6 logged-in custom-background cat-6-id”>  

1、其中数字为该分类的ID号,前提是主题模板必须使用函数:

  1. <body <?php body_class(); ?>>  

2、有了这个非常方便的功能,你就可以针对不同的分类定义不同的样式风格。

3、将以下代码添加到您当前主题的 functions.php 文件:

  1. 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');  

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: