简体   繁体   English

如何按用户角色过滤WordPress主页帖子?

[英]How to filter WordPress home page posts by user role?

Is it possible to filter home page blog posts by user role? 是否可以按用户角色过滤主页博客帖子? My aim is to only display posts written by users of role type 'editor'. 我的目标是只显示角色类型“编辑”用户编写的帖子。

Create a file in your theme's root folder and save it as home.php then paste following code in that file and you are done. 在主题的根文件夹中创建一个文件,并将其另存为home.php然后将以下代码粘贴到该文件中,您就完成了。

<?php
    get_header();
    $ids = get_users( array('role' => 'editor' ,'fields' => 'ID') );
    $args = array(
        'post_type'=>'post',
        'post_status'=>'publish',
        'author' => implode(',', $ids)
    );

    $query = new WP_Query($args);
    if($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
            // code goes here, for example
            echo the_title() . '<br />'; // prints title of each post
        endwhile;
    endif;
    get_sidebar();
    get_footer();
?>

Under your home.php or front-page.php. 在你的home.php或front-page.php下。 add this following code: 添加以下代码:

<?php 

$args = array (
    'role' => 'influencer',
    'fields' => 'ID',
);

$user_ids = get_users($args);

$query_posts = new WP_Query( 
    array( 
        'post_type' => 'post',
        'author__in' => $user_ids
    )
);

var_dump($query_posts);
?>

Note: i just used the var_dump function to show post by author role. 注意:我只是使用var_dump函数来显示作者角色。 You can use the while loop and have_posts of wordpress or pure php loop 你可以使用word循环和纯php循环的while循环和have_posts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM