最新公告
  • 欢迎您光临站盟网(原知事网),一个优质的网站源码基地、精品网站模板和插件。欢迎加入永久SVIP
  • WordPress函数wp_get_post_terms()用法 获取文章分类法数组

    正文概述 知事网   2020-09-13 11:09   849

    描述:

    获取文章的分类法数组

    用法:

    <?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()用法 获取文章分类法数组

    发表评论

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

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者
    请选择支付方式
    ×
    支付宝支付
    微信支付
    余额支付
    ×
    微信扫码支付 0 元