【问题标题】:<style> tag inside a <pre> tag<pre> 标签内的 <style> 标签
【发布时间】:2013-03-09 14:06:46
【问题描述】:

我想知道如何做到这一点。我有这个代码:

// Text
function login_styles() {   
    echo '<style type="text/css">h1 a { background:url('. get_bloginfo("template_directory") .'/images/din_logo.png) no-repeat center top; }</style>';   
}  
add_action('login_head', 'login_styles');

这是一个 WordPress 主题的函数,应该在“functions.php”中。但是当我想将它插入到帖子中以在我的博客上分享时,它不会显示:style type="text/css 等。样式标签内的整个代码都不会显示。

为了显示我在帖子中使用 pre 标记的代码。似乎不会显示标签中的标签?为什么会这样?

【问题讨论】:

  • 您是否在问为什么您的博客文章会呈现 html 标签? php.net/htmlentities
  • 您能否粘贴实际的 html 输出内容,然后再粘贴您想要的内容?我无法从您的问题中推断出这一点。
  • 我希望这可以工作,但没有,jsfiddle.net/kjDc4
  • @marcuscvj 您应该只编辑您的答案以包含该输出,而不是将其放在评论中,作为评论很难阅读
  • @AshBurlaczenko
     标签不会阻止其他标签正常呈现

标签: php html wordpress styles pre


【解决方案1】:
echo htmlentities(..style tag as string goes here..);

【讨论】:

    【解决方案2】:

    &lt;pre&gt; 不会阻止标签成为标签。这只是意味着空白字符(空格、换行符等)不会被折叠成一个空格。

    您需要使用实体或字符引用将&amp;lt;(和其他一些字符)表示为数据,而不是标记。

    htmlspecialchars 函数会将 HTML 中具有特殊含义的字符(例如 &amp;lt; 表示“标记的开始”)转换为将这些字符表示为数据的实体(例如 &amp;lt; 表示“小于字符” )。

    <pre><?php
        echo htmlspecialchars('<style> etc etc </style>');
    ?></pre>
    

    这将给出:

    <pre>&lt;style&gt; etc etc &lt;/style&gt;</pre>
    

    这将呈现为:

    <style> etc etc </style>
    

    【讨论】:

    • 使用以下代码:
      h1 a { background:url('.get_bloginfo("template_directory) ") .'/images/din_logo.png) 不重复中心顶部;}'); } add_action('login_head', 'login_styles'); ?>
      它会给我这个作为输出: h1 a { background:url('.get_bloginfo("template_directory") .'/images/din_logo.png) no-repeat center top; }'); } add_action('login_head', 'login_styles'); ?>
    • 看起来您的输出与输入相同,但减去了&lt;style&gt; 开始标记,加上调用htmlspecialchars 函数后的原始PHP 代码。我不明白这怎么可能。
    猜你喜欢
    • 2016-08-27
    • 1970-01-01
    • 1970-01-01
    • 2013-11-24
    • 1970-01-01
    • 1970-01-01
    • 2019-08-08
    • 1970-01-01
    • 2020-03-16
    相关资源
    最近更新 更多