最新公告
  • 欢迎您光临知事网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入我们
  • WordPress函数wp_get_post_terms()用法 获取文章分类法数组

    正文概述 知事网   2020-09-13   19

    描述:

    获取文章的分类法数组

    用法:

    <?php $terms = wp_get_post_terms( $post_id, $taxonomy, $args ); ?>

    参数:

    $post_id
    
    (integer) (可选) 文章ID
    
    默认值: 0
    
    $taxonomy
    
    (string|array) (可选) 检索分类法。默认为post_tag。
    
    默认值: 'post_tag'
    
    $args
    
    (array) (可选) 覆盖默认值
    
    默认值: array

    示例:

    //Returns All Term Items for "my_taxonomy"
    $term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
    print_r($term_list);
    
    //Returns Array of Term Names for "my_taxonomy"
    $term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "names"));
    print_r($term_list);
    
    //Returns Array of Term ID's for "my_taxonomy"
    $term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "ids"));
    print_r($term_list);
    
    //Echo a single value - $term_list is an array of objects. You must select one of the
    // array entries before you can reference its properties (fields).
    $term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "all"));
    echo $term_list[0]->description ;

    源文件:

    /**
    * Retrieve the terms for a post.
    *
    * There is only one default for this function, called 'fields' and by default
    * is set to 'all'. There are other defaults that can be overridden in
    * {@link wp_get_object_terms()}.
    *
    * @since 2.8.0
    *
    * @param int $post_id Optional. The Post ID. Does not default to the ID of the
    * global $post. Default 0.
    * @param string $taxonomy Optional. The taxonomy for which to retrieve terms. Default 'post_tag'.
    * @param array $args Optional. {@link wp_get_object_terms()} arguments. Default empty array.
    * @return array|WP_Error List of post terms or empty array if no terms were found. WP_Error object
    * if `$taxonomy` doesn't exist.
    */
    function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) {
    $post_id = (int) $post_id;
    
    $defaults = array('fields' => 'all');
    $args = wp_parse_args( $args, $defaults );
    
    $tags = wp_get_object_terms($post_id, $taxonomy, $args);
    
    return $tags;
    }
    找互联网优质资源,上知事网!
    知事网 » WordPress函数wp_get_post_terms()用法 获取文章分类法数组

    发表评论

    发表评论

    • 313会员总数(位)
    • 389资源总数(个)
    • 28本周发布(个)
    • 0 今日发布(个)
    • 3389稳定运行(天)

    提供最优质的资源集合

    Mac软件 Dedecms源码
    升级SVIP尊享更多特权立即升级