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

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

    描述:

    使用代码设置一篇文章的分类

    用法:

    <?php wp_set_post_categories( $post_ID, $post_categories, $append ) ?>

    参数:

    $post_ID
    
    (integer) (可选) 文章ID
    
    默认值: 0
    
    $post_categories
    
    (array) (可选) 分类ID
    
    默认值: array
    
    $append
    
    (boolean) (可选) 如果为true,则邮件将附加类别。如果为false,则类别将替换现有类别。
    
    默认值: false

    源文件:

    /**
    * Set categories for a post.
    *
    * If the post categories parameter is not set, then the default category is
    * going used.
    *
    * @since 2.1.0
    *
    * @param int $post_ID Optional. The Post ID. Does not default to the ID
    * of the global $post. Default 0.
    * @param array|int $post_categories Optional. List of categories or ID of category.
    * Default empty array.
    * @param bool $append If true, don't delete existing categories, just add on.
    * If false, replace the categories with the new categories.
    * @return array|bool|WP_Error
    */
    function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) {
    $post_ID = (int) $post_ID;
    $post_type = get_post_type( $post_ID );
    $post_status = get_post_status( $post_ID );
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if ( empty( $post_categories ) ) {
    if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
    $post_categories = array( get_option('default_category') );
    $append = false;
    } else {
    $post_categories = array();
    }
    } elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) {
    return true;
    }
    
    return wp_set_post_terms( $post_ID, $post_categories, 'category', $append );
    }
    找互联网优质资源,上知事网!
    知事网 » WordPress函数wp_set_post_categories()用法 设置一篇文章的分类

    发表评论

    发表评论

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

    提供最优质的资源集合

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