新行后添加短代码输出

spe*_*.sm 4 wordpress shortcode

我正在尝试创建一个短代码来为页面添加CSS样式属性.我将以下代码添加到我的主题的functions.php中.

function add_style( $atts, $content = null ) {
    return '<style>' . $content . '</style>';
}
add_shortcode( 'style', 'add_style' );
Run Code Online (Sandbox Code Playgroud)

在页面的编辑器中,我用它作为:

[style]
.image-main{
  border:5px solid lightblue;
}
[/style]
Run Code Online (Sandbox Code Playgroud)

在呈现的页面上,它输出到:

<style>
<br />
.image-main{<br />
  border:5px solid lightblue;<br />
}<br />

</style>
Run Code Online (Sandbox Code Playgroud)

<br />当我有多行内容时,如何设置短代码以删除?

Aid*_*ran 11

由于WordPress处理内容的默认顺序,因此插入了br标记 - 在处理短代码之前运行wpautop(将换行符转换为p或br标记的函数).

解决方案:

更改wpautop的执行优先级,以便在处理镜头代码之后执行它而不是之前执行.在functions.php文件中添加:

remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12);
Run Code Online (Sandbox Code Playgroud)

现在,短代码块中不会添加额外的p或br标签.事实上,根本不会将换行符自动转换为p和/或br标签.因此,如果您希望合法换行符转换为p和br标记,则需要从短代码函数中运行wpautop,例如:

function bio_shortcode($atts, $content = null) {
   $content = wpautop(trim($content));
   return '<div class="bio">' . $content . '</div>';
}
add_shortcode('bio', 'bio_shortcode');
Run Code Online (Sandbox Code Playgroud)