wp_get_post_categories()通过文章id获取所属的分类id

目录

描述

译文

检索文章的类型列表。

主题和插件兼容层,也是不具备分类层复杂性的简单抽象层。

原文

The function wp_get_post_categories() retrieve a list of categories for a post.

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

wp_get_post_categories() 描述

用法

<?php wp_get_post_categories$post_id$args ); ?>

wp_get_post_categories() 用法

参数

$post_id

(integer) (可选) The Post ID.

默认值: 0

$args

(array) (可选) Overwrite the defaults.

默认值: array

Default $args are:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
$defaults = array('fields' => 'ids');

wp_get_post_categories() 参数

返回值

(array) 

The array contains a list of category ID's.

wp_get_post_categories() 返回值

示例

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$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 );
}

wp_get_post_categories() 示例

注意

wp_get_post_categories() 注意

历史

添加于 版本: 2.1.0

wp_get_post_categories() 历史

源文件

wp_get_post_categories() 函数的代码位于 wp-includes/post.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the list of categories for a post.
 *
 * Compatibility layer for themes and plugins. Also an easy layer of abstraction
 * away from the complexity of the taxonomy layer.
 *
 * @since 2.1.0
 *
 * @see wp_get_object_terms()
 *
 * @param int   $post_id Optional. The Post ID. Does not default to the ID of the
 *                       global $post. Default 0.
 * @param array $args    Optional. Category arguments. Default empty.
 * @return array List of categories.
 */
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;
}

wp_get_post_categories() 源文件