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

问题描述 投票:1回答:2

是否可以按用户角色过滤主页博客帖子?我的目标是只显示角色类型“编辑”用户编写的帖子。

php wordpress
2个回答
4
投票

在主题的根文件夹中创建一个文件,并将其保存为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();
?>

0
投票

在你的home.php或front-page.php下。添加以下代码:

<?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);
?>

注意:我只是使用var_dump函数来显示作者角色。你可以使用word循环和纯php循环的while循环和have_posts

© www.soinside.com 2019 - 2024. All rights reserved.