最新公告
  • 欢迎您光临知事网软件APP资源下载站,一个优质的手机App应用商店和网站源码基地。欢迎加入永久SVIP
  • WordPress函数wp_get_post_categories()用法 通过文章id获取所属分类id

    正文概述 知事网   2020-09-13 10:09:20   649

    描述:

    通过文章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;
    }

    知事网 » WordPress函数wp_get_post_categories()用法 通过文章id获取所属分类id

    发表评论

    还没有评论,快来抢沙发吧!

    [!--temp.right--]
    请选择支付方式
    ×
    余额支付
    ×
    微信扫码支付 0 元