让WordPress图片自动链接到文章,添加标题和ALT属性,请直接将下面的代码添加到主题的 functions.php 里:
function auto_post_link($content) { global $post; $content = preg_replace('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$2\" alt=\"".$post->post_title."\" /></a>", $content); return $content; } add_filter ('the_content', 'auto_post_link',0);
最终代码输出结果示例:
<a href="https://www.rengang.com.cn/wordpress-img-link-to-post.html" title="让WordPress图片自动链接到文章,添加标题和ALT属性" ><img src="https://www.rengang.com.cn/2015/08/wordpress_img_link_to_post.jpg" alt="让WordPress图片自动链接到文章,添加标题和ALT属性" /></a>
让WordPress图片自动链接到文章,添加标题和ALT属性
https://www.rengang.com.cn/wordpress-img-link-to-post.html
「真诚赞赏,手留余香」
赞助用于本站维护,手机长按识别二维码。

任刚(rengang.com.cn)整理分享,欣赏作品版权均归原作者所有,仅供学习交流。点击上方图标与好友分享!
发表评论
要发表评论,您必须先登录。