删除 WordPress Body 类
如何从 WordPress 的 body 元素中删除类?
默认: body class =“page page-id-7 page-template-default登录”
我正在寻找什么: body class="page-id-7"
在帖子上: 主体类=“postid-40”
How could I remove classes from the body element in Wordpress?
Default:
body class="page page-id-7 page-template-default logged-in"
What I'm looking for:
body class="page-id-7"
On Post's:
body class="postid-40"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是找到类似内容的方法:
通过在主题文件中查找相关函数,在本例中为 header.php
在 WordPress Codex 中查找此函数 (http://codex.wordpress.org/Function_Reference/body_class)
有没有类似的示例我想做什么?是的:
// 通过过滤器添加特定的 CSS 类
add_filter('body_class','my_class_names');
函数 my_class_names($classes) {
// 将“类名”添加到 $classes 数组中
$classes[] = '类名';
// 返回 $classes 数组
返回$classes;
}
类,只需将其添加到functions.php中:
基本上,要删除所有
4. 但我想保留页面 id 和帖子 id - 我该怎么做?因为您不知道可以在数组中的哪个索引上找到此信息,并且在数组中搜索是蹩脚的,所以您需要首先像上面那样清空数组,然后添加您真正想要的内容。如何获得正确的信息,即页面 id 和帖子 id?这是第五步。
(5) 在 Codex 页面的底部,您将找到源代码的链接。您将找到以下链接: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/post-template.php 如果您查看该函数,您会发现它使用了一个简单的函数填充我们可以使用上述过滤器操作相同的数组。通过观察他们是如何做到的,您也可以做到。
希望这段代码能够工作。如果没有,请尝试按照上述步骤找出问题所在。我希望我至少能帮上一点忙:)
This is how you find something like this out:
Find the relevant function by looking in a theme file, in this case header.php
Look this function up in the Wordpress Codex (http://codex.wordpress.org/Function_Reference/body_class)
Is there any examples that look similar to what I want to do? Yes:
// Add specific CSS class by filter
add_filter('body_class','my_class_names');
function my_class_names($classes) {
// add 'class-name' to the $classes array
$classes[] = 'class-name';
// return the $classes array
return $classes;
}
So basically, to remove all classes, just add this to functions.php:
t
4. But I want to keep page id and post id - how can I do that? Because you don't know on what index in the array that you can find this information on, and searching through the array is lame, you need to first empty the array like we did above, and then add the stuff you really want. How do you get the right information, that is, page id and post id? This is step five.
(5) At the bottom of the codex page you will find a link to the source code. This is the link you'll find: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/post-template.php If you look at the function, you'll see it uses a function which simply populates the same array that we can manipulate with aforementioned filter. By looking how they do it, you can do it too.
Hopefully this code works. If it does not, try to figure out what's wrong by following the steps above. I hope I helped at least a little bit :)
当前正在运行的代码用于删除和添加类
currently working code to remove and add a class