WordPress给文章图片自动添加链接的方法

这篇文章主要为大家介绍了WordPress给文章图片自动添加链接的方法,以实例形式分析了自动给图片添加链接以及给关键字添加链接的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了WordPress给文章图片自动添加链接的方法。分享给大家供大家参考。具体分析如下:

我们会看到有很多的网站我们点击图片就会进入当前文件连接了,下面我来给使用wordpress博客的同学也来介绍此种方法,图片自动链接到文章,添加标题和ALT属性.

直接将下面的代码添加到主题的 functions.php 文件即可:

复制代码
代码如下:
function auto_post_link($content) {
global $post;
$content = preg_replace('/]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i', "post_title."" >post_title."" />", $content);
return $content;
}
add_filter ('the_content', 'auto_post_link',0);

最终的输出结果如下:

复制代码
代码如下:

关键词自动添加链接

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

复制代码
代码如下:
function auto_post_link($content) {
global $post;
$content = preg_replace('/]*?srcs*=s*('|")(.*?)\1[^>]*?/?s*>/i', "post_title."" >post_title."" />", $content);

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

希望本文所述对大家的WordPress建站有所帮助。

以上就是WordPress给文章图片自动添加链接的方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » CMS教程