我正在尝试渲染一个 100% HTML5 兼容的 WordPress 主题,并且已经解决了除一个障碍之外的所有问题。
在某些帖子的末尾,我显示了一个“Tweet”链接,该链接在主题模板中使用以下源代码:
<a href="http://twitter.com/share?text=<?php the_title_attribute(); ?>&via=ianhines&url=<?php echo simple_url_shortener('','service=bit.ly+key&apikey=R_a6dc414291bb882024ddd99690f5eb61&login=ianhines&cache=no'); ?>" title="Share This Article on Twitter">Tweet</a>
HTML5 禁止在 URL 中包含空格。它们必须呈现为 %20
。然而,
呈现帖子标题的 XHTML 安全版本,并保留空格。
示例 URL(使用上面的模板源代码呈现):
a href="http://twitter.com/share?text=Twitter, Reblog, and Email Comments&via=ianhines&url=http://ihin.es/eCoYN9" title="Share This Article on Twitter">Tweet</a>
有什么方法可以强制 WordPress 将此 URL 字符串中的空格呈现为 %20,从而使我的网站完全兼容 HTML5?
I'm trying to render a WordPress theme that is 100% HTML5 compliant, and have managed my way through all but one snag.
At the end of certain posts I show a "Tweet" link, which uses the following source code in the Theme template:
<a href="http://twitter.com/share?text=<?php the_title_attribute(); ?>&via=ianhines&url=<?php echo simple_url_shortener('','service=bit.ly+key&apikey=R_a6dc414291bb882024ddd99690f5eb61&login=ianhines&cache=no'); ?>" title="Share This Article on Twitter">Tweet</a>
HTML5 forbids having spaces in URLs. They must be rendered as %20
. However, <?php the_title_attribute; ?>
renders an XHTML safe version of the Post Title with spaces maintained.
An example URL (rendered using the template source code above):
a href="http://twitter.com/share?text=Twitter, Reblog, and Email Comments&via=ianhines&url=http://ihin.es/eCoYN9" title="Share This Article on Twitter">Tweet</a>
Is there any way I can force WordPress to render the spaces in this URL string as %20, and thereby make my site fully HTML5 compliant?
发布评论
评论(4)
好吧,只需用
urlencode()
包裹the_title_attribute()
:编辑:好的,由于该评论,您需要执行以下操作:
编辑2:查看
the_title_attribute
的文档:Well, just wrap the
the_title_attribute()
withurlencode()
:Edit: Ok, due to that comment, you'd need to do something like this:
Edit2: Looking at the docs for
the_title_attribute
:传递给
the_title_attribute()
的值 0 会使其返回而不是回显其结果。A value of 0 passed to
the_title_attribute()
makes it return rather than echo it's result.编辑
我添加了
echo=0
来返回文本而不是显示它,请参阅the_title_attribute
。EDIT
I added
echo=0
to return the text instead of displaying it, seethe_title_attribute
.我的建议是使用
the_title_attribute()
和urlencode()
空格的 echo 参数:My suggestion would be to use the echo param of
the_title_attribute()
andurlencode()
the spaces: