从没有文字内容(使用短代码)的页面中删除自动添加的<p> </p>

时间:2013-03-02 17:23:47

标签: php wordpress

我有一个WordPress支持的网站,在主页上使用一个只有短代码的静态页面来生成内容。

页面通过将首页设置为静态页面并使用the_content()来获取这些短代码;在page.php上。页面内容没有空格,只有短代码,所以看起来像这样:

[content-shortcode blah blah][more content-shortcode blah blah]

一切正常,除了WordPress在短代码代码之前添加空<p></p>而在所有短代码代码末尾添加另一个P / P(短代码之间没有任何内容)。

如何删除它们?我不想禁用全局wpautop删除功能,因为它对某些用户有用,我只想删除主页上显示的第一个和最后一个P。

5 个答案:

答案 0 :(得分:18)

您可以尝试一些事项。

您可以推迟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);

答案 1 :(得分:2)

尝试此操作(将此代码粘贴到functions.php中的某处):

function shortcode_empty_paragraph_fix($content){   
    $array = array (
        '<p>[' => '[', 
        ']</p>' => ']', 
        ']<br />' => ']'
    );

    $content = strtr($content, $array);
    return $content;
}

add_filter('the_content', 'shortcode_empty_paragraph_fix');

答案 2 :(得分:1)

wpautop()force_balance_tags()之外还有各种功能,例如remove_filter('the_content', 'wpautop');,旨在平衡通过编辑器输入的不良HTML。

它们主要在filter post content中定义,您可以在其中查看源代码中的各种代码。

正如您所指出的那样,删除这些过滤器就像一行一样简单:

{{1}}

有关详细信息:formatting.php

查看下面可以帮助您的链接。

  1. http://codex.wordpress.org/Function_Reference/wpautop
  2. Removing <p> and <br/> tags in WordPress posts
  3. wordpress-wrapping-shortcodes-with-p-tags
  4. 这可能对你有所帮助。

答案 3 :(得分:1)

由于您只想删除主页上的<p>标记,请将以下代码添加到functions.php文件中:

add_action('pre_get_posts', 'remove_autop_from_home');
function remove_autop_from_home() {
    // check to see if we are on the home page
    if(is_home()) {
        remove_filter('the_content', 'wpautop');
    }
}

答案 4 :(得分:0)

jQuery('p:empty').remove();

您也可以使用JS。上面的代码将帮助您从页面中删除所有空的p标签