外贸独立站的底层设计密码WordPress 成品站SaaS建站
当前位置:首页>WordPress建站>网站SEO>WordPress 文章图片自动添加当前文章链接

WordPress 文章图片自动添加当前文章链接

应 @李辉 朋友的要求,分享一下为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.wpdaxue.com/wordpress-view-history.html" title="WordPress 添加文章浏览历史功能" >
<img src="https://www.wpdaxue.com/img/2013/03/wpdaxue.com-201303521.png" alt="WordPress 添加文章浏览历史功能" />
</a>

关键词自动添加链接

还可以再添加一个功能,将文章标签作为关键词,将文章内的关键词自动加上链接,有利于SEO,别人复制的时候,就会留下链接了。在上面的函数里继续添加一段代码即可。

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);
	   
	    $posttags = get_the_tags();
	 if ($posttags) {
		 foreach($posttags as $tag) {
			 $link = get_tag_link($tag->term_id); 
			 $keyword = $tag->name;
	   		$content = preg_replace('\'(?!((<.*?)|(<a.*?)))('. $keyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s','<a href="'.$link.'" title="'.$keyword.'">'.$keyword.'</a>',$content,2);//最多替换2个重复的词,避免过度SEO
		 }
	 }
	   return $content;
}
add_filter ('the_content', 'auto_post_link',0);

第一张图片加链接,其他图片加alt属性

一样在functions.php添加:

/*--------------------------------------
自动为第一张图片添加链接地址,其他图片只加alt属性,不加链接
默认链接地址为当前文章的链接,alt属性为当前文章的标题
通过修改判断语句if($count==1),可以为指定顺序图片添加链接,不局限于第一个图片
--------------------------------------*/
$count = 0;
function auto_image_handler($matches){
	global $count,$post;
	$count++;
	if($count==1){//第一个图片添加链接地址
		return "<a href=\"".get_permalink()."\" title=\"".$post->post_title."\" ><img src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
	}
	else{//其他图片添加alt属性
		return "<img src=\"$matches[2]\" alt=\"".$post->post_title."\" /></a>";
	}
}
function auto_post_link($content){
	$content = preg_replace_callback('/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i',
	'auto_image_handler',$content);
	return $content;
}
add_filter ('the_content', 'auto_post_link',0);

参考资料:http://zhaokaihua.com/405.html

实测报告

1.使用上面的代码后,由于替代了图片的多余属性,将导致图片的对齐方式失效;

2.由于使用图片灯箱效果时,需要将图片链接到原始图片的地址,如果使用该方法将图片链接到文章,那么灯箱效果就没办法使用,请自行取舍。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
网站SEO网站维护

WordPress 404错误统计及重定向插件

2013-4-1 9:10:05

WordPress插件网站SEO

WordPress自定义文章类型的固定链接设置插件:Custom Post Type Permalinks

2013-4-24 8:55:04

26 条回复 A文章作者 M管理员
  1. 晓庄

    博主你好,有没有办法添加a标签了以后,保留图片的多余属性,需要一些图片对齐功能

  2. 鼓捣成功了,多谢。

  3. 第一种搞法 成功了
    后两种搞法 网站就打不开了

  4. 如果我是要去掉所有的图片链接,是不是删掉代码里面的a href就可以了啊?

  5. 正是我所需要的,谢谢萌萌!!

  6. 暂时不用,学习了。

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索