洪运源码

WordPress发布文章时如何设置随机作者昵称

你是否发现一个明明是个人网站,为什么有很多发布者。其实这个都是站长一个人发布的,只需要简单设置就可以完成。如何实现发布的时候随机配置多个用户呢。下面直接贴出代码放到我们的主题下的functions.php文件最下面即可。

// 随机选择作者
function random_author() {
    // 获取站点作者
    $authors = get_users( array( 'role' => 'author' ) );
    // 随机选择作者
    $random_author = $authors[ array_rand( $authors ) ];
    return $random_author;
}
// 发布文章随机选择一个作者
add_filter( 'wp_insert_post_data', 'apply_random_author', 10, 2 );
function apply_random_author( $data, $postarr ) {
    if ( $data['post_type'] == 'post' && $data['post_status'] == 'publish' ) {
        $author = random_author();
        $data['post_author'] = $author->ID;
    }
    return $data;
}

洪运小编亲测可以使用。

WordPress发布文章时如何设置随机作者昵称 链接:https://www.hycodes.cn/s/4200.html

返回顶部