自动为WordPress文章添加标签

admin wordpress评论440

将下方的代码复制粘贴到你所使用的主题函数function.php文件中

function zm_auto_add_tags() {
	$post_id = get_the_ID();
	if ( $post_id ) : $post_content = get_post( $post_id )->post_content;
	if ( !empty( $post_content ) ) {
		$tags = get_tags( array( 'hide_empty' => false ) );
		if ( $tags ) {
			$i = 0;
			foreach ( $tags as $tag ) {
				if ( strpos( $post_content, $tag->name ) !== false ) {
					if ( $i == 6 ) break; // 添加数量
					wp_set_post_tags( $post_id, $tag->name, true );
					$i++;
				}
			}
		}
	}
	endif;
}
add_action( 'save_post', 'zm_auto_add_tags' );

注意,此功能如果你的标签数量极多,大于几千个标签数量的时候,发布更新文章时可能会卡死状态文章源自小虫博客-https://www.xczi.cn/wordpress/632.html 文章源自小虫博客-https://www.xczi.cn/wordpress/632.html

admin


  • ·版权声明·



    1、本站文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系客服进行删除处理。
    2、本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    3、如有侵权请邮件admin@xczi.cn与我们联系处理。敬请谅解!


  • 转载请务必保留本文链接:自动为WordPress文章添加标签-https://www.xczi.cn/wordpress/632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: