You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WordPress PHP归档模板代码中为链接添加动态title属性?

给归档模板的图片链接添加动态标题

直接修改你的printf模板字符串,加入title属性占位符,并用get_the_title()获取文章标题,同时用esc_attr()做安全转义,代码如下:

printf('<a href="%s" title="%s">%s</a>', esc_url( $post_link ), esc_attr( get_the_title( $post ) ), get_the_post_thumbnail( $post, 'large' ));

关键说明:

  • get_the_title( $post ):传入$post参数,精准获取当前循环中对应文章的标题
  • esc_attr():对标题内容进行转义处理,防止特殊字符破坏HTML结构,同时避免XSS安全风险
  • 模板里新增的title="%s"会被第二个参数的转义后标题填充,实现动态title效果

内容的提问来源于stack exchange,提问作者Manuel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 14:20:49