【问题标题】:dynamically loading html title attribute动态加载html标题属性
【发布时间】:2012-05-24 15:02:44
【问题描述】:

这是我的标记的摘录:

    <img src="<?php echo get_post_meta($post->ID, 'thumbnail_value', true); ?>" title="<?php the_tags(); ?>" alt="" class="thumb"></a>

我已经输入了“标题”属性&lt;?php the_tags(); ?&gt;,这是来自 Wordpress 的一个函数。 当我检查 Firebug 中的标记时,我看到:

li class="view view_2 view-first">
<a id="50" href="" class="thumb_link" onclick="update_default_proj_list(1, '', '_'); getProject(50);"><span class="selected"></span><img 

src="13370318682.jpg" title="Tags: &lt;a href=" http:="" whiteandgold.ro="" realty="" ?tag="house&quot;" rel="tag">house</a>, <a href="http://

whiteandgold.ro/realty/?tag=rent-3" rel="tag">rent</a>" alt="" class="thumb"&gt;

我试图隐藏该文本:

img.thumb[title] {text-indent:-9999px;}



a[rel~="tag"] { text-indent:-999px;}`

它不起作用,你也可以看到这个here(尝试检查缩略图)

【问题讨论】:

    标签: html css tags title


    【解决方案1】:

    the_tags() 渲染 HTML,所以你不应该把它放在 title 属性中。将它放在任何 HTML 属性之外:

    <div id="thepost">
      // stuff
      <?php the_tags(); ?>
    </div>
    

    如果您出于某种原因仍想这样做,可以使用 htmlspecialchars 使其工作:

    <img src="<?php echo get_post_meta($post->ID, 'thumbnail_value', true); ?>" title="<?php htmlspecialchars(the_tags()); ?>" alt="" class="thumb"></a>
    

    或者您可能只想要一个以逗号分隔的标签列表,在这种情况下,您可以使用get_the_tags() 生成它(它为您提供了一个标签数组,您可以随心所欲地使用它)。

    【讨论】:

    • 我已经尝试过使用 htmlspecialchars 并且没有任何改变。我需要将标签放在标题属性中,因为我有一个仅按标题属性搜索的搜索插件。这里是搜索:jsfiddle尝试输入“abc”
    • @CristoceaCostel - 我怀疑你想使用get_the_tags() 建立一个标签列表,因为它没有所有额外的 HTML 垃圾。如果您按照我的建议执行 htmlspecialchars 操作,您将代码更改为什么以及输出是什么样的?
    • 我会建议 htmlentities 而不是 htmlspecialchars -- 更彻底。 php.net/manual/en/function.htmlentities.php
    猜你喜欢
    • 2016-03-27
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2017-02-06
    • 2012-07-08
    • 2017-09-04
    • 1970-01-01
    相关资源
    最近更新 更多