wordpress代码实现自动为文章添加关键词标签

wordpress代码实现自动为文章添加关键词标签,发布文章时自动生成标签

将以下代码添加到functions.php文件内即可

// WordPress 自动为文章添加已使用过的标签
function array2object($array) { // 数组转对象
if (is_array($array)) {
$obj = new StdClass();
foreach ($array as $key => $val){
$obj->$key = $val;
}
}
else {
$obj = $array;
}
return $obj;
}
function object2array($object) { // 对象转数组
if (is_object($object)) {
foreach ($object as $key => $value) {
$array[$key] = $value;
}
}
else {
$array = $object;
}
return $array;
}
add_action('save_post', 'auto_add_tags');
function auto_add_tags(){
$tags = get_tags( array('hide_empty' => false) );
$post_id = get_the_ID();
$post_content = get_post($post_id)->post_content;
if ($tags) {
$i = 0;
$arrs = object2array($tags);shuffle($arrs);$tags = array2object($arrs);// 打乱顺序
foreach ( $tags as $tag ) {
// 如果文章内容出现了已使用过的标签,自动添加这些标签
if ( strpos($post_content, $tag->name) !== false){
if ($i == 5) { // 控制输出数量
break;
}
wp_set_post_tags( $post_id, $tag->name, true );
$i++;
}
}
}
}
/* 自动为文章内的标签添加内链 */
$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, '$'),__('【查看更多[%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);

<img loading="lazy" class="alignleft size-full wp-image-1626" src="https://www.qumaw.com/wp-content/uploads/2022/03/1646908737-a5022a0ab35a58a.png" width="1493" height="769" srcset="https://www.qumaw.com/wp-content/uploads/2022/03/1646908737-a5022a0ab35a58a.png 1493w, https://www.qumaw.com/wp-content/uploads/2022/03/1646908737-a5022a0ab35a58a-300x155.png 300w, https://www.qumaw.com/wp-content/uploads/2022/03/1646908737-a5022a0ab35a58a-1024x527.png 1024w, https://www.qumaw.com/wp-content/uploads/2022/03/1646908737-a5022a0ab35a58a-768x396.png 768w" sizes="(max-width: 1493px) 100vw, 1493px" title="wordpress代码实现自动为文章添加关键词标签插图1" alt="wordpress代码实现自动为文章添加关键词标签插图1" />
免责声明:秦略网络社区所发布的一切软件资源均来自于互联网仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则一切后果请用户自负;本站所有软件信息来自网络,版权争议与本站无关;您必须在下载后的24个小时之内从您的电脑中彻底删除上述内容如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务;本站为非盈利性站点,网站会员注册费用是您喜欢本站而产生的捐赠赞助支持行为,仅为维持服务器的开支与维护,全凭自愿无任何强求;本站为个人博客,所有资源仅供学习参考,并不贩卖软件不存在任何商业目的及用途,如果您访问和下载此文件,表示您同意只将此文件用于参考、学习而非其他用途如有侵权请邮件与我们联系处理,我们会及时处理
秦略网络社区 » wordpress代码实现自动为文章添加关键词标签

发表评论

欢迎访问秦略网络社区,创新只是起点,服务永无止境!