网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress针对不同页面的自定义类

GG网络技术分享 2025-03-18 16:12 10


问题描述:

I have unique theme for WP and I need to add different classes into tag body if user come to these pages. (Example for example.com/bonuses/ I need tag body with class =\"body page_body\" for example.com/news/ I need tag body with class =\"body page_news\" ) How can I get it ?

In Internet I found tip like -

write in functions.php

function my_body_class_filter( $classes ) {

global $post;

if ( $post ) {

$classes[] = $post->post_name;

}

return $classes;

}

add_filter( \'body_class\', \'my_body_class_filter\' );

and in header.php

<body <?php body_class(); ?> 

But I got a lot of classes https://prnt.sc/jtn3pf I don\'t need so many classes. Can someone recommend a filter by slug for pages ?

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我有WP的独特主题,如果用户访问这些页面,我需要在标签正文中添加不同的类。 (示例以示例说明/我需要使用class =“body page_body”的标签体,例如/ news /我需要带有class =“body page_news”的标签体)我怎样才能得到它?

在互联网上,我在functions.php </ p>

  function my_body_class_filter($ classes){

global $ post;

if($ post){\\ n $ classes [] = $ post-&gt; post_name;

}

return $ classes;

</ code> </ pre>

}

add_filter(\'body_class\',\' my_body_class_filter\'); </ p>

和header.php </ p>

 &lt; body&lt;?php body_class();  ?&GT;  

</ code> </ pre>

但我有很多课程 https:// prnt.sc/jtn3pf 我不需要这么多课程。 有人可以推荐一个针对页面的slug过滤器吗?</ p>

</ div>

网友观点:

If you don\'t want other classes then you need to remove <?php body_class(); ?> from tag.

And add your custom class directly using below way.

 $classname=\\\"body\\\";

global $post;

$classname. = \\\" page_\\\".$post->post_name;

<body <?php echo $classname; ?>>

标签:

提交需求或反馈

Demand feedback