Php 在wordpress中创建短代码-如何允许段落

Php 在wordpress中创建短代码-如何允许段落,php,wordpress,shortcode,Php,Wordpress,Shortcode,我目前正在为我正在开发的wordpress主题创建短代码。我希望它尽可能友好,目前我发现,当我在wordpress编辑器中使用段落分隔短代码时,它会添加不必要的代码 例如,当我在WP编辑器中键入以下内容时: [container] [row] [one_half]1st Half[/one_half] [one_half]2nd Half[/one_half] [/row] [/container] 我在前端得到这个结果: <section class="wrapper spe

我目前正在为我正在开发的wordpress主题创建短代码。我希望它尽可能友好,目前我发现,当我在wordpress编辑器中使用段落分隔短代码时,它会添加不必要的代码

例如,当我在WP编辑器中键入以下内容时:

[container]

[row]

[one_half]1st Half[/one_half]

[one_half]2nd Half[/one_half]

[/row]

[/container]
我在前端得到这个结果:

<section class="wrapper special container style3"></p>
<p><div class="row"></p>
<p><div class="6u"><section>1st Half</section></div></p>
<p><div class="6u"><section>2nd Half</section></div></p>
<p></div></p>
<p></section>
结果是正确的……如下所示:

<section class="wrapper special container style3">
<div class="row">
    <div class="6u">
        <section>1st Half</section>
    </div>
    <div class="6u">
        <section>2nd Half</section>
    </div>
</div>
</section>

上半场
下半场
下面是我的短代码的php(上面提到的三个):

//容器
函数容器($atts,$content=null){
$return\u string=''.do\u短代码($content)。'';
wp_reset_query();
return$return\u字符串;
}
//划船
函数行($atts,$content=null){
$return\u string=''.do\u短代码($content)。'';
wp_reset_query();
return$return\u字符串;
}
函数一对半($atts,$content=null){
$return_字符串='';
$return\u string.=''.do\u短码($content.);
$return_字符串='';
wp_reset_query();
return$return\u字符串;
}
函数寄存器_短码(){
添加_短代码(“行”、“行”);
添加_短码('one_half','one_half');
添加_短代码('container','container');
}
添加_操作('init','register_shortcode');

我想要的是能够像第一个示例那样在wp编辑器中编写我的短代码(因为用户这样查看它更自然),但要像第二个示例那样正确地输出代码。有什么办法可以做到这一点吗?

问题不在于你的代码,而在于Wordpress的可视化编辑器,它可以自动添加额外的段落标记(更准确地说,段落标记是在显示时添加的,而不是在存储时添加的)

您可以:

  • 使用文本视图(HTML视图)以换行符所需的方式粘贴短代码

  • 试试这个插件


如果要在文章中添加标记,则不应使用可视化编辑器。找到一种通过自定义过滤器添加标记的方法,这样可以避免插入帖子,或者停止使用可视化编辑器

可视化编辑器适用于不知道标记是什么的人

但是,将此代码添加到模板页面顶部会起作用(或者将其放入模板的
functions.php
文件中):

上述解决方案不会影响现有帖子,只会影响新帖子。


<?php remove_filter ('the_content', 'wpautop'); ?>
如果要从每个内容中删除标记,请将上述代码添加到functions.php文件中


否则,请在要删除标记的文件中使用此代码。

如果不使用wordpress editor中的可视化编辑器,您还会得到其他标记吗?@nk-47我得到了一些,但没有那么多-但我也不想强迫用户使用文本编辑器而不是可视化编辑器
// Container

function container($atts, $content = null) {
   $return_string = '<section class="wrapper special container style3">'. do_shortcode($content) .'</section>';

    wp_reset_query();
    return $return_string;
}

// Row

function row($atts, $content = null) {
   $return_string = '<div class="row">'. do_shortcode($content) .'</div>';

    wp_reset_query();
   return $return_string;
}

function one_half($atts, $content = null) {
    $return_string .= '<div class="6u">';
    $return_string .= '<section>'. do_shortcode($content) .'</section>';
    $return_string .= '</div>';

    wp_reset_query();
   return $return_string;
}


function register_shortcodes(){
   add_shortcode('row', 'row');
   add_shortcode('one_half', 'one_half');
   add_shortcode('container', 'container');

}
add_action( 'init', 'register_shortcodes');
<?php remove_filter ('the_content', 'wpautop'); ?>