【问题标题】:WordPress Amend Body ClassWordPress 修改正文类
【发布时间】:2014-10-25 12:05:00
【问题描述】:

我想将 -wp 添加到 WordPress 使用 http://codex.wordpress.org/Function_Reference/body_class 添加的所有正文类的开头

这是因为我有时想使用 CSS 文档根目录中的 .attachment 或 .page 之类的类,因为它是 body 类,所以会发生冲突。

法典向我展示了如何将类添加到正文中,但没有告诉我如何添加到它添加的每个单独的类中。

【问题讨论】:

    标签: html css wordpress class


    【解决方案1】:

    你可以通过在 body_class 钩子上添加一个动作来做到这一点。

    add_filter( 'body_class', 'my_class_names' );
    function my_class_names( $classes ) {
      // You have all the classes in $classes
      // Iterate and add the wp prefix.
      $new_classes = array();
      foreach($classes as $cls) {
        $new_classes[] = 'wp-' . $cls;
      }
    
       return $new_classes;
    }
    

    代码未经测试,但应该可以工作。

    【讨论】:

    • 警告:array_unique() 期望参数 1 为数组,在第 682 行的 C:\xampp\htdocs\wordpress\wp-includes\post-template.php 中给出 null 警告:join():第 499 行 C:\xampp\htdocs\wordpress\wp-includes\post-template.php 中传递的参数无效
    • 这会正常输出它们——我需要对这段代码做些什么吗?如果您的意思是我需要输入课程或者它会自动获取课程,我不确定“您在 $classes 中拥有所有课程”?
    • 它应该会自动获取它们。当然是body_class钩子存在。测试函数,看看参数给你的。
    • 它得到了他们所有:single single-post postid-93 single-format-standard logged-in admin-bar no-customize-support - 它只是不添加 wp-
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多