我以前的博客作者在博客文章的每一句话上都使用了他的字体系列和大小。我想将新的css规则应用到帖子中,但它不能覆盖这些内嵌样式。
我试着四处搜索,发现了这个:
add_filter( \'the_content\', function( $content ){
return str_replace( \' style="\', \' data-style="\', $content);
});
但它删除了整个网站的风格。我只是想删除的风格,对单一的帖子的内容只使用。我以前的博客作者在博客文章的每一句话上都使用了他的字体系列和大小。我想将新的css规则应用到帖子中,但它不能覆盖这些内嵌样式。
我试着四处搜索,发现了这个:
add_filter( \'the_content\', function( $content ){
return str_replace( \' style="\', \' data-style="\', $content);
});
但它删除了整个网站的风格。我只是想删除的风格,对单一的帖子的内容只使用。您想从单篇文章中删除样式。“单一职位”一词令人困惑。无论您是需要用于单个帖子还是单个帖子模板。
无论哪种情况,您都必须将代码与条件一起使用,您的代码可能如下所示。
If you want to remove from all posts(not pages but including custom post types).
add_filter( \'the_content\', function( $content ){
if(is_single()){
return str_replace( \' style="\', \' data-style="\', $content);
}
else{
return $content;
}
});
If you want to remove from all posts(only WordPress post, not custom post type or pages).
add_filter( \'the_content\', function( $content ){
if (is_singular(\'post\')) {
return str_replace( \' style="\', \' data-style="\', $content);
}
else{
return $content;
}
});
is_singular
WordPress API是否有条件检查帖子类型。https://developer.wordpress.org/reference/functions/is_singular/ If you want to remove from only one particular post.
add_filter( \'the_content\', function( $content ){
if(is_single([POST-ID])){
return str_replace( \' style="\', \' data-style="\', $content);
}
else{
return $content;
}
});
你可以试试这个代码,效果更好
add_filter(\'the_content\', function( $content ){
$content = preg_replace(\'/ style=("|\\\')(.*?)("|\\\')/\',\'\',$content);
return $content;
}, 20);
我正在使用下面相同的代码对许多其他样式进行排队,但它不适用于Elementor和其他一些样式。function enqueue_content(){ wp_dequeue_style( \'elementor-icons-shared-0\' ); wp_dequeue_style( \'elementor-icons-fa-solid\' ); wp_dequeue_style( \'elementor-icons-fa-regular\' ); add_ac