返回介绍

wp_set_post_categories()

发布于 2017-09-11 12:53:02 字数 2848 浏览 970 评论 0 收藏 0

wp_set_post_categories( int $post_ID,  array|int $post_categories = array(),  bool $append = false )

Set categories for a post.


description

If the post categories parameter is not set, then the default category is going used.


参数

$post_ID

(int) (Optional) The Post ID. Does not default to the ID of the global $post. Default 0.

$post_categories

(array|int) (Optional) List of categories or ID of category.

Default value: array()

$append

(bool) (Optional) If true, don't delete existing categories, just add on. If false, replace the categories with the new categories.

Default value: false


返回值

(array|false|WP_Error) Array of term taxonomy IDs of affected categories. WP_Error or false on failure.


源代码

File: wp-includes/post.php

function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
	$post_ID = (int) $post_ID;
	$post_type = get_post_type( $post_ID );
	$post_status = get_post_status( $post_ID );
	// If $post_categories isn't already an array, make it one:
	$post_categories = (array) $post_categories;
	if ( empty( $post_categories ) ) {
		if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
			$post_categories = array( get_option('default_category') );
			$append = false;
		} else {
			$post_categories = array();
		}
	} elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) {
		return true;
	}

	return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
}

更新日志

Versiondescription
2.1.0Introduced.

相关函数

Uses

  • wp-includes/option.php: get_option()
  • wp-includes/post.php: wp_set_post_terms()
  • wp-includes/post.php: get_post_type()
  • wp-includes/post.php: get_post_status()

Used By

  • wp-admin/includes/taxonomy.php: wp_create_categories()
  • wp-includes/deprecated.php: wp_set_post_cats()
  • wp-includes/post.php: wp_insert_post()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_setPostCategories()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文