繁体   English   中英

移除<p>和<br> WordPress 帖子中的标签</p>

[英]Removing <p> and <br/> tags in WordPress posts

每当我在 WordPress 帖子页面中发布一些内容时,它都会显示一些段落标签,例如<p><br/> 这在 output 中显示了一些额外的空间。 那么有什么解决办法吗? 如何删除所有标签?

这是因为 WordPress 的wpautop 只需在主题的函数中添加以下代码行。php 文件

remove_filter( 'the_content', 'wpautop' );

remove_filter( 'the_excerpt', 'wpautop' );

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

我不建议使用 remove_filter 选项,因为它会删除您在模板中调用它们的任何地方的标签和标记。

最好的方法是通过 PHP 清理你的字符串

您可以使用 php function strip_tags 删除无用的标记: echo strip_tags(get_the_excerpt()); // Get the raw text excerpt from the current post echo strip_tags(get_the_excerpt()); // Get the raw text excerpt from the current post

或者

echo strip_tags(category_description()); // Get the raw text cat desc from the current cat page

这将在调用它时从当前 wordpress 帖子中删除所有 HTML 标签。

您还可以添加修剪 function 以防万一: echo trim(strip_tags(get_the_excerpt())); // Get the raw text excerpt from the current post echo trim(strip_tags(get_the_excerpt())); // Get the raw text excerpt from the current post

或者

echo trim(strip_tags(category_description())); // Get the raw text cat desc from the current cat page

这非常适合获取 meta="description" 的原始文本和不推荐 HTML 标记的标题。

希望能帮助到你

尝试这个

$my_postid = $post->ID;
$content_post = get_post($my_postid);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = strip_tags($content, '<p><br/>');
echo $content;

在编辑器初始化之前使用此代码删除<p></p>

function tinymce_remove_root_block_tag( $init ) {
    $init['forced_root_block'] = false; 
    return $init;
}
add_filter( 'tiny_mce_before_init', 'tinymce_remove_root_block_tag' );

删除wpautop过滤器不是最灵活的解决方案。 如果您正在寻找逐页解决方案,或逐页发布,您可以使用此插件:

https://wordpress.org/plugins/dont-muck-my-markup/

它将在每个帖子/页面上添加一个复选框,供您选择是否为该特定页面禁用核心 Wordpress 行为。

当您想发布一些 HTML 代码并且自动生成的<br><p>弄乱了您精心设计的设计时,这尤其有用。

该插件还有一个全局选项,因此您可以在所有页面/帖子上默认关闭此行为。

另一种插件是https://wordpress.org/plugins/preserve-code-formatting/但是我只尝试了我描述的那个。

使用这个 $editor= wp_filter_nohtml_kses($_POST['editor1']);

由于此处接受的答案在 WP 4.8.1 中对我不起作用,因此我发布了一个对我有用的解决方法。 我只需要在 p 中添加一个 class 并且 WordPress 不会删除它。

<p class="whatever">

默认情况下,WordPress 将段落<p></p>标签添加到类别描述中。 通过将以下内容添加到您的函数来停止此操作。php 文件

// Remove p tags from category description
remove_filter('term_description','wpautop');

简单易行(无代码)。

谢谢

有时在主题的函数中删除 WordPress 的 wpautop function。php 不起作用(由于某些原因)。

所以我有另一个解决方案如何停止删除<br>标签或双 ( <br><br> ) 换行符。

  1. 更改您的文件/wp-content/themes/your_theme_name/functions.php

在函数顶部添加 2 行。

remove_filter('the_content', 'wpautop');
remove_filter('the_excerpt', 'wpautop');

这将首先关闭wpautopop function。

  1. 在 function wpautop /wp-includes/formatting.php

    A) function wpautop( $pee, $br = true)更改为function wpautop( $pee, $br = false)

    这还将从所有位置关闭wpautopop function。

    B) 改变$pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee);

    $pee1 = $pee; $pee = preg_replace('|<br\s*/?>\s*<br\s*/?>|', "\n\n", $pee); $pee = $pee1;

    这将防止系统删除双<br>标签。 (我知道代码很奇怪但很简单//$pee在这里没有帮助,因为?>标记)。

    C) 改变$pee = preg_replace("/\n\n+/", "\n\n", $pee); //$pee = preg_replace("/\n\n+/", "\n\n", $pee);

    这将防止系统删除多个换行符。

    D)改变这个:

     $pee = preg_replace('?<p>\s*(</.'. $allblocks, '[^>]*>),'; "$1", $pee);

    对此:

     //$pee = preg_replace('?<p>\s*(</.'. $allblocks, '[^>]*>),'; "$1", $pee);

    这将防止系统在<div><article>等打开之后或关闭块元素标记之前删除换行符。

    E)改变这个:

     $pee = preg_replace('?(</.'. $allblocks, '[^>]*>)\s*</p>,'; "$1", $pee);

    对此:

     //$pee = preg_replace('?(</.'. $allblocks, '[^>]*>)\s*</p>,'; "$1", $pee);

    几乎一样:这将防止系统在开始之后或结束块元素标记(如<div><article>等)之前删除换行符。

    F)改变这个:

     $pee = preg_replace('?<br />(\s*</?(:,p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>),'; '$1', $pee);

    对此:

     // $pee = preg_replace('?<br />(\s*</?(:,p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>),'; '$1', $pee);

    这将防止系统删除块末尾的<br>

    G)改变这个:

     $pee = preg_replace('?(</.'. $allblocks, '[^>]*>)\s*<br />,'; "$1", $pee);

    对此:

     //$pee = preg_replace('?(</.'. $allblocks, '[^>]*>)\s*<br />,'; "$1", $pee);

    这将防止系统在打开或关闭块标记之后删除<br>

希望它会有所帮助。 并阅读此文件中的注释——它们将帮助您了解需要打开或关闭的内容。

如果您想删除 Wordpress 帖子或 textarea 中的 p 标签而不更改 function,您可以执行一个简单的 css 技巧,例如:css:

.container >p {
display:none;
}

html:

<div class = 'container'>
<!the p tag u dont want here!>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM