关键字文章采集器(.php文件中保存:代码中注释的很清楚)

优采云 发布时间: 2022-01-20 05:22

  关键字文章采集器(.php文件中保存:代码中注释的很清楚)

  一、标签自动创建链接

  在当前主题的functions.php文件中加入如下代码保存:

  /* 自动为文章内的标签添加内链开始 */

$match_num_from = 1; //一篇文章中同一个标签少于几次不自动链接

$match_num_to = 1; //一篇文章中同一个标签最多自动链接几次

function tag_sort($a, $b){

if ( $a->name == $b->name ) return 0;

return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;

}

function tag_link($content){

global $match_num_from,$match_num_to;

$posttags = get_the_tags();

if ($posttags) {

usort($posttags, \"tag_sort\");

foreach($posttags as $tag) {

$link = get_tag_link($tag->term_id);

$keyword = $tag->name;

$cleankeyword = stripslashes($keyword);

$url = \"<a href=\\\"$link\\\" title=\\\"\".str_replace(\'%s\',addcslashes($cleankeyword, \'$\'),__(\'View all posts in %s\')).\"\\\"\";

$url .= \' target=\"_blank\"\';

$url .= \">\".addcslashes($cleankeyword, \'$\').\"</a>\";

$limit = rand($match_num_from,$match_num_to);

$content = preg_replace( \'|(]+>)(.*)(\'.$ex_word.\')(.*)(]*>)|U\'.$case, \'$1$2%&&&&&%$4$5\', $content);

$content = preg_replace( \'|()|U\'.$case, \'$1$2%&&&&&%$4$5\', $content);

$cleankeyword = preg_quote($cleankeyword,\'\\\'\');

$regEx = \'\\\'(?!((]*?</a>))\\\'s\' . $case;

$content = preg_replace($regEx,$url,$content,$limit);

$content = str_replace( \'%&&&&&%\', stripslashes($ex_word), $content);

}

}

return $content;

}

add_filter(\'the_content\',\'tag_link\',1);

/* 自动为文章内的标签添加内链结束 *

  代码中的注释很清楚,可以自行更正。

  二、指定关键词自动内部链接

  在当前主题的functions.php文件中加入如下代码保存:

  /* 文章指定关键词添加内链开始 */

function replace_text_wps($text){

$replace = array(

\'主题模板库\' => \'<a href=\"https://www.ztmbk.com/\" target=\"_blank\" title=\"主题模板库\">主题模板库</a>\',

\'主题模板库\' => \'<a href=\"https://www.ztmbk.com/\" target=\"_blank\" title=\"主题模板库\">主题模板库</a>\',

\'教程\' => \'<a href=\"https://www.ztmbk.com/jiaocheng/\" target=\"_blank\" title=\"教程\">教程</a>\'

);

$text = str_replace(array_keys($replace), $replace, $text);

return $text;

}

add_filter(\'the_content\', \'replace_text_wps\');

  您可以将代码中的 关键词 和链接替换为您自己的。您需要将该行代码与其他 关键词 一起复制。 >锚文本的数量,只要关键词出现在文章中,不管多少次都会添加一个链接

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线