wp_set_post_categories() 函数用于为指定的文章设置分类。以下是该函数的语法:
wp_set_post_categories( $post_id, $categories, $append );
其中,$post_id 是要设置分类的文章的 ID,$categories 是一个包含分类 ID 的数组,表示要为文章设置的分类,$append 是一个可选的布尔值,表示是否在原有分类的基础上追加新的分类,默认为 false。
以下是一个示例代码,用于将 ID 为 123 的文章设置为分类 ID 1 和 2:
$post_id = 123;
$categories = array( 1, 2 );
wp_set_post_categories( $post_id, $categories );
在上面的代码中,我们首先定义了要设置分类的文章的 ID 和要设置的分类 ID,然后调用 wp_set_post_categories() 函数将这些分类设置为文章的分类。由于 $append 参数默认为 false,因此这个操作会替换原有的分类。如果要追加新的分类,可以将 $append 参数设置为 true,例如:
wp_set_post_categories( $post_id, $categories, true );
在上面的代码中,我们将 $append 参数设置为 true,这样就会在原有的分类的基础上追加新的分类。


0 个评论