【问题标题】:add css class or id before a wordpress post/page在 wordpress 帖子/页面之前添加 css 类或 id
【发布时间】:2014-04-13 21:31:51
【问题描述】:

我正在构建一个 wordpress 插件,但我的插件工作需要一个特定的类到 html 代码(javascript linkify),但所有模板主题都使用不同的类来格式化帖子,例如:

<div class="post_content">

    <div class="content">

如何通过我的插件嵌入我自己的特定 div 类来识别帖子或页面?

【问题讨论】:

    标签: wordpress wordpress-theming linkify


    【解决方案1】:

    查看 body_class()post_class() 的 Codex。如果主题支持它(并且每个编写良好的主题都应该支持它),您可以将自己的类添加到正文或帖子中。

    function my_plugin_class($classes) {
        if ( my_plugin_is_loaded() ) {
            $classes[] = 'my-plugin-class';
        }
        return $classes;
    }
    add_filter('post_class', 'my_plugin_class');    // add the class to the post content
    add_filter('body_class', 'my_plugin_class');    // add the class to the body tag
    

    您可以使用is_page()is_single() 区分页面和帖子

    【讨论】:

    • +1 post_class() 不错,不幸的是,我们无法确定主题作者是否添加了它...尽管如此,没有什么可以做的。
    • 这项工作,但是 post_class 在
    • 您也可以使用 the_content 过滤器:codex.wordpress.org/Plugin_API/Filter_Reference/the_content 此过滤器允许格式化发布和添加任何内容,the_content 让我很开心,非常感谢大家的回答
    • 这是我现在使用 the_content 过滤器的代码: function roll_over_link_class($content){ $str = '";返回$新内容; } add_filter('the_content', 'roll_over_link_class');
    【解决方案2】:

    使用正文类过滤器。这将为 body 元素添加一个类。

    例如

    function wpse_plugin_add_body_class( $classes ) {
        if ( my_plugin_conditional() ) {
            $classes[] = 'my-plugin-class';
        }
    
        return $classes; 
    }
    add_filter( 'body_class', 'wpse_plugin_add_body_class' );
    

    请务必将条件函数更改为您需要的任何内容。然后像这样添加你的 CSS:

    .my-plugin-class #content {
        color: #ff0000;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      相关资源
      最近更新 更多