我们会看到有很多地网站我们点击图片就会进入当前文件连接了,下面我来给使用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(''(?!((<.*?)|(
}
}
return $content;
}
add_filter ('the_content', 'auto_post_link',0);
更多信息请查看IT技术专栏