网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 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()."".$post->post_title."" ><img src="$2".$post->post_title."" /></a>", $content);
    return $content;
    }
    add_filter ('the_content', 'auto_post_link',0);
    最终的输出结果如下:
    代码如下:
    <a href="wordpress-view-history.html" >
    <img src="201303521.png" />
    </a>
    关键词自动添加链接
    还可以再添加一个功能,将文章标签作为关键词,将文章内的关键词自动加上链接,有利于SEO,别人复制的时候,就会留下链接了。在上面的函数里继续添加一段代码即可
    代码如下:
    function auto_post_link($content) {
    global $post;
    $content = preg_replace('/<s*imgs+[^>]*?srcs*=s*('|")(.*?)1[^>]*?/?s*>/i', "<a href="".get_permalink()."".$post->post_title."" ><img src="$2".$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.'">'.$keyword.'</a>',$content,2);//最多替换2个重复的词,避免过度SEO
    }
    }
    return $content;
    }
    add_filter ('the_content', 'auto_post_link',0);
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/17 13:04:52