是否可以在 Wordpress 帖子中注释掉 HTML 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3344270/
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:36:43  来源:igfitidea点击:

Is It Possible to Comment Out HTML Code in a Wordpress Post?

wordpresseditorcommentshtml-entities

提问by user249493

Sometimes I need to inject some raw HTML code into a Wordpress post, and sometimes I need to comment out a chunk of that code.!

有时我需要将一些原始 HTML 代码注入到 Wordpress 帖子中,有时我需要注释掉该代码的一部分。!

With a plain text editor, I can just use <!-- Comment -->around the chunk I want to hide.

使用纯文本编辑器,我可以<!-- Comment -->在想要隐藏的块周围使用。

But when I try this in a WP post, it does hide the code but I still see the "closing comment tag" -->.

但是当我在 WP 帖子中尝试此操作时,它确实隐藏了代码,但我仍然看到“结束评论标签” -->

What's the right way, if possible, to comment out code in a WP post?

如果可能,在 WP 帖子中注释掉代码的正确方法是什么?

Thanks!

谢谢!

回答by jharrell

wpautop() contains a bug that breaks comments containing HTML code. An easy workaround is to add a second opening HTML comment tag just before the closing - this tricks WordPress into working like you would expect it to. see http://core.trac.wordpress.org/ticket/2691

wpautop() 包含一个错误,该错误会破坏包含 HTML 代码的注释。一个简单的解决方法是在结束之前添加第二个打开的 HTML 注释标签 - 这会诱使 WordPress 像您期望的那样工作。见http://core.trac.wordpress.org/ticket/2691

This will work in WordPress:

这将适用于 WordPress:

<!-- <div class="book floatleft"><a href="#">
<img src="http://www.myreallycoolsite.com/wp-content/uploads/2013/02/button.png" alt="" />
</a></div> <!-- -->

This will not work in WordPress:

这在 WordPress 中不起作用:

<!-- <div class="book floatleft"><a href="#">
<img src="http://www.myreallycoolsite.com/wp-content/uploads/2013/02/button.png" alt="" />
</a></div> -->

回答by ecume des jours

Use a hidden div block

使用隐藏的 div 块

like this:

像这样:

<div style="display: none;">

...comment...

</div>

works like a charm

奇迹般有效

回答by RobertPitt

Instead of typeing <!--Comment-->in the editor for your post, Make sure you place the comment tag inside the raw html editor.

不要<!--Comment-->在编辑器中输入您的帖子,请确保将评论标签放在原始 html 编辑器中。

alt text
(source: headwaythemes.com)

替代文字
(来源:headwaythemes.com

Also use a DOM Inspector to make sure that th -->closing tag is actually coming form the post itself.

还可以使用 DOM Inspector 来确保-->结束标签实际上来自帖子本身。

Another Tip, before you publish the article, hit the Close Tagsbutton to make sure that it validates your html better.

另一个提示,在您发布文章之前,点击Close Tags按钮以确保它更好地验证您的 html。

回答by SiteSubscribe

You could try one of the following plugins which preserves code formatting within the html editor:

您可以尝试使用以下插件之一来保留 html 编辑器中的代码格式:

  • TRUEedit Plugin
  • WP Super Edit
  • ps-disable-auto-formatting
  • Unfiltered MU(multisite only)
  • TRUEedit 插件
  • WP超级编辑
  • ps-禁用-自动格式化
  • 未过滤的 MU(仅限多站点)

I believe most of these plugins removes the wptexturize filter that WordPress uses which replaces characters and patterns (which messes up some shortcodes and html).

我相信这些插件中的大多数都删除了 WordPress 使用的 wptexturize 过滤器,它替换了字符和模式(这会弄乱一些短代码和 html)。

If your using 'Deans FCKEditor' or 'Foliopress WYSIWYG' that could be the problem since they convert quotes to html quotes, add paragraph markup, mess up shortcodes, and do some other html character replacement.

如果您使用“Deans FCKEditor”或“Foliopress WYSIWYG”,这可能是问题所在,因为它们会将引号转换为 html 引号、添加段落标记、弄乱短代码并进行其他一些 html 字符替换。

回答by Justin Sternberg

This snippet should do what you're looking for.

此代码段应该可以满足您的要求。

// Add the unfiltered_html capability back in to WordPress 3.0 multisite.
function um_unfilter_multisite( $caps, $cap, $user_id, $args ) {
if ( $cap == 'unfiltered_html' ) {
    unset( $caps );
    $caps[] = $cap;
}
return $caps;
}
add_filter( 'map_meta_cap', 'um_unfilter_multisite', 10, 4 );

回答by flash1821

Try this:

尝试这个:

<!-- Comment --!>

Works like a charm.

奇迹般有效。

回答by William

Like jharrel suggested, this works just fine:

就像 jharrel 建议的那样,这很好用:

<!-- content <!-- -->

回答by userboy

Try this:

尝试这个:

&lt;!&#45;&#45;<br />
... commented out stuff ...<br >
&lt;&#45;&#45;&gt;

but beware the HTML break tag WordPress will throw in at the end of the comment.

但要注意 WordPress 会在评论末尾插入 HTML 中断标记。