返回介绍

wp_get_post_categories()

发布于 2017-09-11 12:07:35 字数 3936 浏览 1019 评论 0 收藏 0

wp_get_post_categories( int $post_id,  array $args = array() )

Retrieve the list of categories for a post.


description

Compatibility layer for themes and plugins. Also an easy layer of abstraction away from the complexity of the taxonomy layer.


参数

$post_id

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

$args

(array) (Optional) Category query parameters. See WP_Term_Query::__construct() for supported arguments.

Default value: array()


返回值

(array|WP_Error) List of categories. If the $fields argument passed via $args is 'all' or 'all_with_object_id', an array of WP_Term objects will be returned. If $fields is 'ids', an array of category ids. If $fields is 'names', an array of category names. WP_Error object if 'category' taxonomy doesn't exist.


源代码

File: wp-includes/post.php

function wp_get_post_categories( $post_id = 0, $args = array() ) {
	$post_id = (int) $post_id;

	$defaults = array('fields' => 'ids');
	$args = wp_parse_args( $args, $defaults );

	$cats = wp_get_object_terms($post_id, 'category', $args);
	return $cats;
}

更新日志

Versiondescription
2.1.0Introduced.

More Information

The results from wp_get_post_categories() aren’t cached which will result in a database call being made every time this function is called. Use this function with care. For performance, functions like get_the_category() should be used to return categories attached to a post.


相关函数

Uses

  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: wp_get_object_terms()

Used By

  • wp-admin/includes/post.php: bulk_edit_posts()
  • wp-includes/deprecated.php: wp_get_post_cats()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_getPostCategories()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mt_publishPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_getPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::mw_getRecentPosts()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_getPost()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::blogger_getRecentPosts()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_page()
  • Show 4 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 5You must log in to vote on the helpfulness of this note Contributed by Codex

    The example below shows how categories are retrieved, and then additional information is retrieved for each category.

    
    $post_categories = wp_get_post_categories( $post_id );
    $cats = array();
    	
    foreach($post_categories as $c){
    	$cat = get_category( $c );
    	$cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
    }
    
    

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

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

发布评论

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