本文实例讲述了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给文章图片自动添加链接的方法》的一些看法。更多内容请查看本栏目更多内容!
复制代码代码如下://获取文章评论数,不蕴含作者本人function get_comments_number_filter_author() { g...
在 WordPress 后盾,用户是依照用户名排序的,并且没有显示注册工夫,假如咱们心愿可以在后盾看到用户的注册...
找到wp-includes/formatting.php文件中复制代码代码如下:// This is not a tag, nor is the texturization ...
前言 最近在应用WordPress的时分遇到了一些谬误提醒,置信大家在应用wordpress建设网站的时分,都会遇到一些...
间接上代码吧 复制代码代码如下:<title><?phpif(is_category()){ single_cat_title();}elseif(is_single() ...
我将这个分享进去,假如当前有谁需求能够间接下载上面插件 应用办法: 在cnblogs抉择备份数据,导出一个XML...