7

我有一个 WordPress 驱动的网站,在主页上使用一个静态页面,除了短代码之外什么都没有来生成内容。

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

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

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

我怎样才能删除它们?我不想禁用全局 wpautop 删除功能,因为它对某些用户很有用,我只想删除出现在主页上的第一个和最后一个 P。

4

5 回答 5

20

您可以尝试几件事。

您可以推迟,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);
于 2013-03-15T14:54:11.627 回答
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');
于 2013-03-20T13:49:50.997 回答
2

除了过滤帖子内容wpautop()之外,还有各种功能,例如,旨在平衡通过编辑器进入的不良 HTML。force_balance_tags()

它们主要在formatting.php中定义,您可以在其中查看源代码中的各种代码。

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

remove_filter('the_content', 'wpautop');

更多信息:http ://codex.wordpress.org/Function_Reference/wpautop

查看下面的链接,这将对您有所帮助。

  1. 删除 WordPress 帖子中的 <p> 和 <br/> 标签
  2. wordpress-wrapping-shortcodes-with-p-tags
  3. 禁用 wpautop 插件

这可以帮助你。

于 2013-03-19T09:14:32.827 回答
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');
    }
}
于 2013-03-21T23:02:04.467 回答
0
jQuery('p:empty').remove();

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

于 2019-10-01T09:02:54.497 回答