禁止 WordPress 添加 <p> 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11248628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:57:32  来源:igfitidea点击:

Disable WordPress from adding <p> tags

wordpress

提问by user1048676

All I've got is the following little snippet of code:

我所拥有的只是以下一小段代码:

<select size="1" name="EventHour<?php echo $i; ?>"> 
    <option>1</option> 
    <option>2</option> 
    <option>3</option> 
    <option>4</option> 
    <option>5</option> 
    <option>6</option> 
    <option>7</option> 
    <option>8</option> 
    <option>9</option> 
    <option>10</option> 
    <option>11</option> 
    <option>12</option> 
  </select> 
  : <!-- note this character -->
  <select size="1" name="EventMinute<?php echo $i; ?>"> 
    <option>00</option> 
    <option>05</option> 
    <option>10</option> 
    <option>15</option> 
    <option>20</option> 
    <option>25</option> 
    <option>30</option> 
    <option>35</option> 
    <option>40</option> 
    <option>45</option> 
    <option>50</option> 
    <option>55</option> 
  </select> 

The should output fine. However, WordPress adds a p-tag around both of my select-elements as well as around the ":"-character. This makes them all end up on different rows.

应该输出罚款。但是,WordPress 在我的两个选择元素以及“:”字符周围添加了一个 p 标签。这使得它们最终都在不同的行上。

I've installed and activated the WordPress plugin "Disable Visual Editor WYSIWYG" on this page without any success. Any other ideas what I can do to stop this from happening? Thanks in advance!

我已经在此页面上安装并激活了 WordPress 插件“禁用可视化编辑器所见即所得”,但没有成功。我还能做些什么来阻止这种情况发生?提前致谢!

回答by Mirko

Use this:

用这个:

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

in your functions.php

在你的 functions.php

Here's the complete answer: http://codex.wordpress.org/Function_Reference/wpautop#Disabling_the_filter

这是完整的答案:http: //codex.wordpress.org/Function_Reference/wpautop#Disabling_the_filter

回答by pixelistik

Wordpress modifies and cleans your entered HTML both in the editor and at output.

Wordpress 会在编辑器和输出中修改和清理您输入的 HTML。

Use this plugin to get unmodified markup into your posts:

使用此插件将未经修改的标记添加到您的帖子中:

https://wordpress.org/extend/plugins/raw-html/

https://wordpress.org/extend/plugins/raw-html/

回答by Lucas Zardo

Try this in your functions.php

在你的functions.php中试试这个

<?php remove_filter ('the_content', 'wpautop'); ?>

回答by maker3

You can minify your code. Wordpress won't destroy code if everything is on one line.

您可以缩小代码。如果所有内容都在一行上,Wordpress 不会破坏代码。

I do that when I want to put <style>or <script>tags inside certain posts.

当我想在某些帖子中放置<style><script>标记时,我会这样做。

回答by Ylama

In my case, I'm doing it manually for the page:

就我而言,我正在为页面手动执行此操作:

Result that showsextra ptags:

显示额外p标签的结果:

 <p><?php if($description){ echo $description; } ?></p> 
 <p><?php if($description){ echo wpautop($description); } ?></p> 

Result that removesextra ptags:

删除额外p标签的结果:

 <?php if($description){ echo wpautop($description); } ?>  

Note, I removed the ptags around the echo, then added wpautopto the echo.

请注意,我删除了p周围的标签echo,然后添加wpautopecho.

End Result:

最终结果:

<p>description content</p> 

回答by Ivan Asenov

If the post is custom post type, you can add meta box with add_meta_boxand there you can initialize your own editor with wp_editorwhich can be customized. For example you can pass settings to the tinymce like 'force_p_newslines' and 'forced_root_block'

如果帖子是自定义帖子类型,您可以使用add_meta_box添加元框,然后您可以使用可自定义的wp_editor初始化您自己的编辑器。例如,您可以将设置传递给 tinymce,如“force_p_newslines”和“forced_root_block”

回答by Malik Zahid

At least with contact-form-7 I had some success using a html-minifier to prevent those pesky p-tags. Came here because I googled if others also find this as frustrating as I do.

至少在 contact-form-7 中,我使用 html-minifier 来防止那些讨厌的 p 标签取得了一些成功。来到这里是因为我在谷歌上搜索了其他人是否也像我一样感到沮丧。

If you still have problem after code minifire then use just convert your tag to divwhere ptag is automatically add.

如果您仍然有后代码minifire的问题,那么使用时只需将您的标签div,其中p标签被自动添加。

回答by Jared

if anybody else is having this problem and can't or doesn't want to install another plugin, or again can't or doesn't want to have to edit their functions.php file, you can also do this!

如果其他人遇到这个问题并且不能或不想安装另一个插件,或者不能或不想编辑他们的functions.php文件,你也可以这样做!

My original code:

我的原始代码:

your original code

that worked wonderfully..

and then wordpress happened

My code with the workaround:

我的代码与解决方法:

your original code
//
that worked wonderfully..
//
and then wordpress happened

So long as I make sure to not have any empty lines in my code this seems to be a workaround for me at least. I'm wondering if the :wasn't enough to trick it not to add a p tag, but I'm coding in javascript and it worked for me.

只要我确保我的代码中没有任何空行,这至少对我来说似乎是一种解决方法。我想知道这是否:不足以欺骗它不添加 ap 标签,但我正在用 javascript 编码并且它对我有用。