我正在尝试呈现一个 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
.然而,<?php the_title_attribute; ?>
呈现一个保留空格的帖子标题的 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 标准?
最佳答案
好吧,只需用 urlencode()
包装 the_title_attribute()
:
/share?text=<?php echo urlencode(the_title_attribute()); ?>&via=
编辑:好的,由于该评论,您需要执行以下操作:
<?php
ob_start();
the_title_attribute();
$title = ob_get_clean();
?>
/share?text=<?php echo urlencode($title); ?>&via=
Edit2: 查看 the docs for the_title_attribute
:
/share?text=<?php echo urlencode(the_title_attribute('echo=0')); ?>&via=
关于php - 我如何制作 WordPress <?php the_title attribute(); ?> 为空间渲染 %20?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4768262/