关于之前使用的一个WordPress为文章自动添加标签的一个功能,后续在使用期间,发布文章的时候会跳出一个错误Warning: Attempt to read property “post content” on nul in,下面为大家放上正确的使用代码。
WordPress自动添加标签代码:
/* 自动为文章添加标签 */
add_action('save_post', 'xy_add_tags');
function xy_add_tags( $post_id ){
// 获取文章内容
$post_content = get_post_field( 'post_content', $post_id );
// 获取所有标签
$tags = get_tags( array('hide_empty' => false) );
if ($tags) {
foreach ( $tags as $tag ) {
// 如果文章内容中包含已使用过的标签,自动添加这些标签
if ( strpos($post_content, $tag->name) !== false){
wp_set_post_tags( $post_id, $tag->name, true );
}
}
}
}

在wp主题编辑里面找到“函数模板”名为“functions.php”文件添加进去即可。











