【问题标题】:wordpress widget addng extra p tagswordpress 小部件添加额外的 p 标签
【发布时间】:2016-01-12 11:56:04
【问题描述】:

我有一个这样的短代码:

function requestaquote($atts, $content = null){
    extract(shortcode_atts(array(
        'text'=>'',
        'link'=>'',
        'colour'=>''
        ), $atts));     
    return '<div class="speed-button"><img src="'.get_stylesheet_directory_uri().'/images/request-a-quote.jpg" alt="request a quote " /><p class="requstaquote">'.esc_attr($text).'</p></div><!--speed-button-->';
}
add_shortcode( 'quotetext', 'requestaquote' );

除了引入额外的 &lt;p&gt;&lt;/p&gt; 对之外,它的工作原理如下:

<div class="textwidget">
<p></p>
<div class="speed-button">...</div>
<p></p>
</div>

这弄乱了我的格式。

我试过remove_filter('the_content', 'wpautop');

如何删除这些 &lt;p&gt;&lt;/p&gt; 对。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    只需删除新行,因为新行似乎是被编成段落的内容。

    return str_replace("\r\n", '', $content);
    

    【讨论】:

    • 对不起 - 没用 - 我认为它是因为它在实际短代码之前和之后添加
    • 最后一个简单的事情是让 p 标签显示 none。不是一个完美的解决方案,但它会有所帮助
    【解决方案2】:

    您可以推迟wp_autop,因为它在短代码输出之前处理:

    remove_filter( 'the_content', 'wpautop' );
    add_filter( 'the_content', 'wpautop' , 12);
    

    或者使用cleanup_shortcode_fix()函数:

    function cleanup_shortcode_fix($content) {
        $array = array('<p>[' => '[', ']</p>' => ']', ']<br />' => ']', ']<br>' => ']');
        $content = strtr($content, $array);
        return $content;
    }
    
    add_filter('the_content', 'cleanup_shortcode_fix');
    $string = preg_replace('/<p>s*</p>/', '', $string);
    add_filter('the_content', 'cleanup_shortcode_fix', 1);
    

    来源:Remove auto added <p> from a page that has no literal content (uses shortcodes)

    【讨论】:

    • 我认为您的 clean_up_shortcode_fix 函数看起来很有希望。我复制到 funtions.php 并得到错误:调用未定义的函数 preg_replace_() 有什么想法吗?
    • 谢谢 - 我现在得到... preg_replace(): Unknown modifier 'p'
    • 好的,不要使用cleanup_shortcode_fix函数。只需在您的functions.php文件中添加remove_filter( 'the_content', 'wpautop' ); add_filter( 'the_content', 'wpautop' , 12);并检查。
    • 感谢您抽出时间 Milap - 但是当我尝试时也没有用。
    猜你喜欢
    • 2012-12-25
    • 1970-01-01
    • 2012-06-30
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-06-15
    • 2018-03-09
    相关资源
    最近更新 更多