Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How to achive this in sidebar.List of 10 latest permalinks(post titles) from contributors ?

share|improve this question
add comment

1 Answer

you can use the author parameter of WP_Query

here is a simple function to get you going with a hook to a shortcode and another hook to render this shortcode inside a simple text widget:

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

Paste this code in your theme's functions.php and then just enter [ten_latest_con] inside a text widget.

share|improve this answer
    
Internal Server Error when i put code in functions.php –  user4490 Jul 17 '11 at 20:45
    
Create a pastebin of your functions.php –  Bainternet Jul 18 '11 at 4:55
    
pastebin.com/33hYM0DF –  user4490 Jul 18 '11 at 7:29
    
Ok i've updated the code, there were a few syntax errors but now its tested and works fine. –  Bainternet Jul 18 '11 at 8:22
    
tnx, I will try this code, but Im curious about insert small excerpt toghether with post titles, and maybe user photo also..Is this hard task to accomplish? –  user4490 Jul 19 '11 at 10:15
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.