在新行后添加 <br /> 的简码输出
Shortcode output adding <br /> after new line
我正在尝试创建一个短代码以将 CSS 样式属性添加到页面。我将以下代码添加到我的主题 functions.php.
function add_style( $atts, $content = null ) {
return '<style>' . $content . '</style>';
}
add_shortcode( 'style', 'add_style' );
在页面的编辑器中,我将其用作:
[style]
.image-main{
border:5px solid lightblue;
}
[/style]
在呈现的页面上它输出到:
<style>
<br />
.image-main{<br />
border:5px solid lightblue;<br />
}<br />
</style>
当我有多行内容时,如何设置简码以删除 <br />
?
我发现我的问题的解决方案是使用 str_replace();
替换 remove "<br /r>"
function add_style( $atts, $content = null ) {
$pure_content = str_replace("<br />","",$content);
return '<style>' . $pure_content . '</style>';
}
add_shortcode( 'style', 'add_style' );
br 标签被插入是因为 WordPress 处理您的内容的默认顺序 – wpautop(将换行符转换为 p 或 br 标签的函数)是 运行 在处理短代码之前。
解决方法:
更改 wpautop 的执行优先级,使其在处理拍摄代码之后而不是之前执行。将此添加到您的 functions.php 文件中:
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12);
现在您的短代码块中不会添加额外的 p 或 br 标签。事实上,根本不会自动将换行符转换为 p and/or 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');
我正在尝试创建一个短代码以将 CSS 样式属性添加到页面。我将以下代码添加到我的主题 functions.php.
function add_style( $atts, $content = null ) {
return '<style>' . $content . '</style>';
}
add_shortcode( 'style', 'add_style' );
在页面的编辑器中,我将其用作:
[style]
.image-main{
border:5px solid lightblue;
}
[/style]
在呈现的页面上它输出到:
<style>
<br />
.image-main{<br />
border:5px solid lightblue;<br />
}<br />
</style>
当我有多行内容时,如何设置简码以删除 <br />
?
我发现我的问题的解决方案是使用 str_replace();
"<br /r>"
function add_style( $atts, $content = null ) {
$pure_content = str_replace("<br />","",$content);
return '<style>' . $pure_content . '</style>';
}
add_shortcode( 'style', 'add_style' );
br 标签被插入是因为 WordPress 处理您的内容的默认顺序 – wpautop(将换行符转换为 p 或 br 标签的函数)是 运行 在处理短代码之前。
解决方法:
更改 wpautop 的执行优先级,使其在处理拍摄代码之后而不是之前执行。将此添加到您的 functions.php 文件中:
remove_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'wpautop' , 12);
现在您的短代码块中不会添加额外的 p 或 br 标签。事实上,根本不会自动将换行符转换为 p and/or 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');