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

时间:2014-04-25 07:51:07

标签: php wordpress shortcode

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

例如,当我在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>

如果我写这个:

[container][row][one_half]1st Half[/one_half][one_half]2nd Half[/one_half][/row][/container] 

它出来了......就像这样:

<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(上面提到的三个):

// 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');

我想要的是能够在wp编辑器中编写我的短代码,就像第一个例子一样(因为它对用户来说更加自然,就像这样),但是输出的代码就像在第二个例子。有没有办法实现这个目标?

3 个答案:

答案 0 :(得分:0)

问题不在于您的代码,而在于Wordpress&#39;可视编辑器,可自动添加其他标记(更准确地说,段落标记会在显示时添加,而不是在存储时添加)。

你可以:

  • 使用文字视图(HTML视图)以您想要的方式粘贴短信代码。

    OR

  • 试用Disable Automatic P Tags插件。

答案 1 :(得分:0)

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

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

但是,在模板页面顶部添加此代码可以使用(或将其放在模板的functions.php文件中):

<?php remove_filter ('the_content', 'wpautop'); ?>

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

答案 2 :(得分:0)

<?php remove_filter ('the_content', 'wpautop'); ?>

如果要从每个内容中删除

标记,请将以上代码添加到functions.php文件中。

否则,请在要删除

标记的文件中使用此代码。

相关问题