如何在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
相关产品推荐
相关产品推荐

