删除 WordPress Body 类

发布于 2024-12-10 10:27:40 字数 176 浏览 0 评论 0原文

如何从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寄居者 2024-12-17 10:27:40

这就是找到类似内容的方法:

  1. 通过在主题文件中查找相关函数,在本例中为 header.php

  2. 在 WordPress Codex 中查找此函数 (http://codex.wordpress.org/Function_Reference/body_class)

  3. 有没有类似的示例我想做什么?是的:

    // 通过过滤器添加特定的 CSS 类
    add_filter('body_class','my_class_names');
    函数 my_class_names($classes) {
    // 将“类名”添加到 $classes 数组中
    $classes[] = '类名';
    // 返回 $classes 数组
    返回$classes;
    }

类,只需将其添加到functions.php中:

add_filter('body_class','my_class_names');
function my_class_names($classes) {
    return array();
}

基本上,要删除所有
4. 但我想保留页面 id 和帖子 id - 我该怎么做?因为您不知道可以在数组中的哪个索引上找到此信息,并且在数组中搜索是蹩脚的,所以您需要首先像上面那样清空数组,然后添加您真正想要的内容。如何获得正确的信息,即页面 id 和帖子 id?这是第五步。

(5) 在 Codex 页面的底部,您将找到源代码的链接。您将找到以下链接: http://core.trac.wordpress.org/browser/tags/3.2.1/wp-includes/post-template.php 如果您查看该函数,您会发现它使用了一个简单的函数填充我们可以使用上述过滤器操作相同的数组。通过观察他们是如何做到的,您也可以做到。

add_filter('body_class','my_class_names');
function my_class_names($classes) {
    global $wp_query;

    $arr = array();

    if(is_page()) {
    $page_id = $wp_query->get_queried_object_id();
    $arr[] = 'page-id-' . $page_id;
    }

    if(is_single()) {
    $post_id = $wp_query->get_queried_object_id();
    $arr[] = 'postid-' . $post_id;
    }

    return $arr;
}

希望这段代码能够工作。如果没有,请尝试按照上述步骤找出问题所在。我希望我至少能帮上一点忙:)

This is how you find something like this out:

  1. Find the relevant function by looking in a theme file, in this case header.php

  2. Look this function up in the Wordpress Codex (http://codex.wordpress.org/Function_Reference/body_class)

  3. 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:

add_filter('body_class','my_class_names');
function my_class_names($classes) {
    return array();
}

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.

add_filter('body_class','my_class_names');
function my_class_names($classes) {
    global $wp_query;

    $arr = array();

    if(is_page()) {
    $page_id = $wp_query->get_queried_object_id();
    $arr[] = 'page-id-' . $page_id;
    }

    if(is_single()) {
    $post_id = $wp_query->get_queried_object_id();
    $arr[] = 'postid-' . $post_id;
    }

    return $arr;
}

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 :)

青朷 2024-12-17 10:27:40

当前正在运行的代码用于删除和添加类

//remove body class

add_filter('body_class', function (array $classes) {
    if (in_array('class_name', $classes)) {
      unset( $classes[array_search('class_name', $classes)] );
    }
  return $classes;
});

// Add specific CSS class by body.
 add_filter( 'body_class', function( $classes ) {
    return array_merge( $classes, array( 'class-name' ) );
} );

currently working code to remove and add a class

//remove body class

add_filter('body_class', function (array $classes) {
    if (in_array('class_name', $classes)) {
      unset( $classes[array_search('class_name', $classes)] );
    }
  return $classes;
});

// Add specific CSS class by body.
 add_filter( 'body_class', function( $classes ) {
    return array_merge( $classes, array( 'class-name' ) );
} );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文