仅显示来自具有参与者角色的用户的10篇最新帖子

时间:2011-07-17 作者:user4490

如何在边栏中实现这一点。来自贡献者的10个最新永久链接(帖子标题)列表?

1 个回复
SO网友:Bainternet

您可以使用author 的参数WP_Query

下面是一个简单的函数,可以让您使用一个指向短代码的钩子,并使用另一个钩子在一个简单的文本小部件中呈现此短代码:

function ten_latest_of_contributers($atts = null){
global $post;
//store the current post data
$temp = $post;
$contributors = get_users(array(\'role\'=> \'contributor\'));
//then create an array of contributors ids

$contributors_ids = array();
foreach ($contributors as $user) {
    $contributors_ids[] =  $user->ID;
}

//then create a new query and use the author parameter
$con_query = new WP_Query(array(
    \'author\' => implode( \',\', $contributors_ids ),
    \'post_type\' => \'post\',
    \'posts_per_page\' => 10));

//all that is left is to loop
$re = \'<ul>\';
while($con_query->have_posts()){
    $con_query->the_post();
    $re .= \'<li><a href="\'.get_permalink($post->ID).\'">\'.get_the_title($post->ID).\'</a></li>\';
}
$re .= \'</ul>\';
//reset the current post data
wp_reset_postdata();
$post = $temp;

//return the list
return $re;
}
// hook your shortcode
add_shortcode(\'ten_latest_con\',\'ten_latest_of_contributers\');

//render shortcodes in widgets
add_filter(\'widget_text\', \'do_shortcode\');
将此代码粘贴到主题的函数中。php,然后只需输入[ten_latest_con] 在文本小部件中。

结束