php - 我如何制作 WordPress <?php the_title attribute(); ?> 为空间渲染 %20?

标签 php wordpress html standards

我正在尝试呈现一个 100% HTML5 兼容的 WordPress 主题,并且已经设法解决了所有问题,只有一个障碍。

在某些帖子的末尾,我显示了一个“Tweet”链接,它在主题模板中使用了以下源代码:

<a href="http://twitter.com/share?text=<?php the_title_attribute(); ?>&amp;via=ianhines&amp;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()); ?>&amp;via=

编辑:好的,由于该评论,您需要执行以下操作:

<?php
ob_start();
the_title_attribute();
$title = ob_get_clean();
?>
/share?text=<?php echo urlencode($title); ?>&amp;via=

Edit2: 查看 the docs for the_title_attribute :

/share?text=<?php echo urlencode(the_title_attribute('echo=0')); ?>&amp;via=

关于php - 我如何制作 WordPress <?php the_title attribute(); ?> 为空间渲染 %20?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4768262/

相关文章:

wordpress : how to add categories and tags on pages?

html - 如何使用 CSS 将选择下拉列表中的文本对齐方式移向顶部和左侧?

javascript - 如何验证具有多个选项卡的表单?

javascript - 使用 jQuery 定位 div 中的类并在同一 div 中执行其他更改

php - 使用 PHP 检查远程文件是否为格式正确的 XML

具有不同元素的 PHP 数组(如 Python 集合)

php - PHP 有没有好的图像处理 API(ImageMagick 替代品)?

php - 如何从 img 标签下载图像?

css - div 后面的下拉菜单

php - 发送有关 woocommerce 购物车更新的额外数据