WordPress纯代码实现自动为文章添加标签及标签内链接

WordPress纯代码实现自动为文章添加标签及标签内链接,第1张

WordPress纯代码实现自动为文章添加标签及标签内链接

即然我们都知道Tag标签的功效,可是還是有许多网站站长不以文章添加Tag标签,关键缘故就是说每一次公布文章必须手动式为文章添加Tag标签,真的是太不便了.

那么有没有一个木头会让WordPress站点自动给文章添加之前应用的Tag标签呢?有没有一个链接可以让WordPress站点自动给文章的Tag标签添加链接,并把链接变成内部链接?这两个问题的参考答案毋庸置疑。今天把WordPress站点分享给大家,不断自动给文章添加标签,自动给这些标签添加链接成为内部链接。

WordPresspureencoding保留了自动添加文章标签的方式:只有以下编码必须添加到主题的最后一个functions.php文档中?>你可以。

/*自动标记文章*/
add_action('save_post','auto_add_tags');
functionauto_add_tags(){
$tags=get_tags(array('hide_empty'=>;false));
$post_ID=get_the_ID();
$post_content=get_post($post_id)->;post_content
If($tags){
foreach($tagsas$tag){
/如果一篇文章的内容中出现了应用的标签,这个标签会自动添加
[name]!==false)
WP_set_post_tags($post_id,$tag->;名,真);
}
}
}

左右编码的功能是指当人们发布/存储/升级一篇文章时,会自动检查文章的内容,看是否有之前应用的标签。如果出现,它会自动将此标签添加到文章中。如果人民的站点有一个标签:主持人评价,那么人民的文章内容如果有主持人评价,就会自动给文章添加一个“主持人评价”标签。

WordPress纯编码保持了文章中标签自动添加内部链接的方式:下面的编码是添加到主题的最后一个functions.php文档中的吗?>你可以。

/*自动为文章内的标签添加内部链接*/
$match_num_from=1;//一篇文章中同一个标签低于几回不自动连接
$match_num_to=1;//一篇文章中同一个标签数最多自动连接几回
functiontag_sort($a,$b){
if($a->name==$b->name)return0;
return(strlen($a->name)>strlen($b->name))?-1:1;
}
functiontag_link($content){
global$match_num_from,$match_num_to;
$posttags=get_the_tags();
if($posttags){
usort($posttags,“tag_sort”);
foreach($posttagsas$tag){
$link=get_tag_link($tag->term_id);
$keyword=$tag->name;
$cleankeyword=stripslashes($keyword);
$url=“<ahref=\”$link\”title=\””.str_replace(‘%s’,addcslashes($cleankeyword,‘$’),__(‘【查询带有[%s]标签的文章】’)).”\””;
$url.=‘target=”_blank”‘;
$url.=“>”.addcslashes($cleankeyword,‘$’).”</a>”;
$limit=rand($match_num_from,$match_num_to);
$content=preg_replace(‘|(<a[^>]+>)(.*)(‘.$ex_word.’)(.*)(</a[^>]*>)|U’.$case,‘$1$2%&&&&&%$4$5’,$content);
$content=preg_replace(‘|(<img)(.*?)(‘.$ex_word.’)(.*?)(>)|U’.$case,‘$1$2%&&&&&%$4$5’,$content);
$cleankeyword=preg_quote($cleankeyword,’\”);
$regEx=‘\'(?!((<.*?)|(<a.*?)))(‘.$cleankeyword.‘)(?!(([^<>]*?)>)|([^>]*?</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);/*自动为文章中的标签添加内部链接*/
$match_num_from=1;//一篇文章中的同一个标签多次不自动连接
$match_num_to=1;//一篇文章中相同数量的标签最多可以自动连接几次
functiontag_sort($a,$b){
if($a->;name==$b->;name)返回0;
return(strlen($a->;姓名)>strlen($b->;姓名))?-1:1;
}
functiontag_link($content){
global$match_num_from,$match_num_to
$posttags=get_the_tags();
if($posttags){
usort($posttags,"tag_sort");
foreach($posttagsas$tag){
$link=get_tag_link($tag->;term_id);
$keyword=$tag->;姓名;
$cleankeyword=stripslashes($keyword);
$URL="<;Href=\"$link\"title=\"str_replace('%s',addcslashes($cleankeyword,'$'),__('[查询带有[%s]标记的文章]')。"\";
$url。='target="_blank
$url。=">。addcslakes($cleankeyword,'$')。“</a>;;
$limit=rand($match_num_from,$match_num_to);
$content=preg_replace('|(<;a[^>;]+>;)(.*)('.$ex_word。')(.*)(<;/a[^>;]*>;)|U'。$case,$1$2%&;&&&&%',$content);
$content=preg_replace('|(<;img)(。*?)('.$ex_word。')(.*?)(>)|U'。$case,$1$2%&;&&&&%',$content);
$cleankeyword=preg_quote($cleankeyword,'\");
$regEx='\'(?!((<。*?)|(<;答.*?)))('.$cleankeyword。)(?!((【^<;>]*?)>)|([^>;]*?</a>;))\'s'。$case
$content=preg_replace($regEx,$url,$content,$limit);
$content=str_replace('%&;&&&&%',stripslasts($ex_word),$content);
}
}
return$content;
}
add_filter('the_content','tag_link',1);

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/744463.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-29
下一篇 2022-04-29

发表评论

登录后才能评论

评论列表(0条)

保存