本文实例讲述了WordPress给文章图片主动增加链接的办法。分享给大家供大家参考。详细剖析如下:
咱们会看到有很多的网站咱们点击图片就会进入以后文件衔接了,上面我来给应用wordpress博客的同窗也来引见此种办法,图片主动链接到文章,增加题目和ALT属性.
间接将上面的代码增加到主题的 functions.php 文件即可:
function auto_post_link($content) {
global $post;
$content = preg_replace('/<s*imgs+[^>]*?srcs*=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="wordpress-view-history.html" title="WordPress 增加文章阅读历史性能" >
<img src="201303521.png" alt="WordPress 增加文章阅读历史性能" />
</a>
还能够再增加一个性能,将文章标签作为要害词,将文章内的要害词主动加上链接,无利于SEO,他人复制的时分,就会留下链接了,在下面的函数里持续增加一段代码即可:
function auto_post_link($content) {
global $post;
$content = preg_replace('/<s*imgs+[^>]*?srcs*=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);
心愿本文所述对大家的WordPress建站有所协助。
以上就是安达网络工作室关于《WordPress给文章图片自动添加链接的方法》的一些看法。更多内容请查看本栏目更多内容!
WordPress呈现“需求晋级数据库”普通是网站搬家,或许WordPress版本晋级会呈现。普通状况呈现的...
4月更新过文章,文章ID停留在146,5月忙于考试,文章一篇都没写,后果6月前几天写文章的时分,ID从146间接跳...
本文实例讲述了WordPress完成搜寻后果包括自定义文章类型内容的办法。分享给大家供大家参考。详细剖析如下:...
在WordPress上增加中文用户时报错说不支持中文,失去如下谬误:谬误:请填写用户名。谬误:此用户名蕴含有效...
wp_list_categories 函数是 WordPress 中用来列举零碎中分类的函数,该函数领有许多管制输入的参数,明天忽...
很多时分,咱们做网站时在自身网站没有齐全架构好就去寻觅所谓的SEO技巧。这往往是轻重倒置。就拿wordpress...