描述:
通过文章id获取所属的分类id
用法:
<?php wp_get_post_categories( $post_id, $args ); ?>
参数:
$post_id
(integer) (可选) 文章ID
默认值: 0
$args
(array) (可选) 覆盖默认值。
默认值: array
Default $args are:
示例:
$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 );
}
源文件:
/**
* 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;
}
发表评论
还没有评论,快来抢沙发吧!