【问题标题】:Creating shortcodes in wordpress - how do I allow paragraphs在 wordpress 中创建简码 - 我如何允许段落
【发布时间】:2014-04-25 07:51:06
【问题描述】:

我目前正在为我正在开发的 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 编辑器中编写我的短代码(因为用户这样查看它更自然),但输出的代码像第二个示例一样正确。有什么方法可以实现吗?

【问题讨论】:

  • 如果你不使用wordpress编辑器中的可视化编辑器,你还会得到额外的

    标签吗?
  • @nk-47 我得到了一些但没有那么多 - 但我也不想强迫我的用户使用文本编辑器而不是可视化编辑器

标签: php wordpress shortcode


【解决方案1】:

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

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

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

&lt;?php remove_filter ('the_content', 'wpautop'); ?&gt;

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

【讨论】:

    【解决方案2】:

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

    您可以:

    • 使用文本视图(HTML 视图)以您想要的方式粘贴短代码并使用换行符。

    • 试试Disable Automatic P Tags 插件。

    【讨论】:

      【解决方案3】:
      <?php remove_filter ('the_content', 'wpautop'); ?>
      

      如果要删除上面的代码,请将上述代码添加到functions.php文件中

      标记每个内容。

      否则在要删除的文件中使用此代码

      标签。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 2011-02-06
        相关资源
        最近更新 更多