• Resolved JC Palmes

    (@khleomix)


    I’ve been trying to show an author list on my sidebar with an avatar and the author’s latest posts. Its basically working but I want to restrict the list to just the users set as contributors and nothing else. Right now it shows all users in the blog. Any help would be greatly appreciated. Thanks.

    <div class="author">
    <?php
    //get all users, iterate through users, query for one post for the user,
    //if there is a post then display the post title, author, content info
    $blogusers = get_users_of_blog();
    
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $args = array(
              'author' => $bloguser->user_id,
    	  'showposts' => 1,
    	  'caller_get_posts' => 1,
        );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div style="border-top:1px solid #bababa; margin:0">
    <?php echo get_avatar( get_the_author_email(), '80' ); ?>
    
    <h4 style="padding-top:10px;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br /><small><?php the_author_posts_link(); ?> </small></h4>
    		<?php
            the_excerpt();
    		?>
            <p class="more-link"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" style="color: rgb(157, 20, 20); font-weight: bold;"><?php _e('Read', 'news'); ?></a> | Comments (<?php comments_number('', '1', '%'); ?>)</p>
            </div>
     <?     endwhile;
        }
    
      }
    }
    
    ?>
    
    </div>
    <div style="clear:both;"></div>
Viewing 8 replies - 1 through 8 (of 8 total)
  • MichaelH

    (@michaelh)

    Thread Starter JC Palmes

    (@khleomix)

    Hi Michael, thanks but I don’t think that’s what I’m looking for… I found some codes and tweaked it up, it is now showing just the contributors but the thing is the title of the posts are the same but one excerpt is correct. Can you help me fix this up? Thanks! You can see the code in action here: https://www.swarfblog.com/new/

    <div class="author">
    <?php $blogusers = get_users_of_blog(); ?>
    
    <?php
    $latest_posts = array();
    foreach ( $blogusers as $bloguser ) {
    $user = new WP_User( $bloguser->ID );
    if ($user->has_cap('level_7'))
    continue;
    $ps = get_posts('showposts=1&post_type=post&author=' . $bloguser->ID . '');
    foreach ($ps as $p) {
    $latest_posts[$p->post_date] = $p;
    }
    }
    ?>
    <?php //query_posts('author=' . $author->ID . '&showposts=1&cat=-9,-3'); ?>
    <?php
    $counter =0;
    foreach ($latest_posts as $post) {
    $counter++;
    if ($counter > 4)
    break;
    setup_postdata($post);
    
    ?>
    
    <?php // while (have_posts()) : the_post(); ?>
    
    <div style="border-top:1px solid #bababa; margin:0">
    <?php echo get_avatar( get_the_author_email(), '80' ); ?>
    <h4 style="padding-top:10px;"><a title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><br /><small><?php the_author_posts_link(); ?></small></h4>
    		<?php
            the_excerpt();
    		?>
    
    <p class="more-link"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" style="color: rgb(157, 20, 20); font-weight: bold;"><?php _e('Read', 'news'); ?></a> | Comments (<?php comments_number('', '1', '%'); ?>)</p>
            </div>
    
    <?php } ?>
    
    <?php // }; ?>
    </div>
    <div style="clear:both;"></div>
    MichaelH

    (@michaelh)

    I’d say you don’t need this line of code and it’s ending }

    <?php // while (have_posts()) : the_post(); ?>

    Thread Starter JC Palmes

    (@khleomix)

    Hi Michael,

    That code and its ending was already commented out and does nothing for the code… What the code does now is get the correct latest post excerpt per user but the title changes according to the category of a page/post that you go into… What I need is to have a the latest post title of the author and it should not change regardless of the category you’re in…

    Thanks

    MichaelH

    (@michaelh)

    Took your original code and used that:

    <?php
    //get users of contibutor role, iterate through users, query for four posts for the user,
    //if there are posts then display the post title, author, content info
    $blogusers = $wpdb->get_col($wpdb->prepare("SELECT wpusers.ID
        FROM $wpdb->users wpusers, $wpdb->usermeta wpusermeta
        WHERE wpusers.ID = wpusermeta.user_id
        AND wpusermeta.meta_key = 'wp_capabilities'
        AND wpusermeta.meta_value RLIKE '[[:<:]]contributor[[:>:]]'
        ORDER BY wpusers.user_nicename ASC"));
    if ($blogusers) {
      foreach ($blogusers as $bloguser) {
        $args = array(
        'author' => $bloguser,
    	  'showposts' => 4,
    	  'caller_get_posts' => 1,
        );
    
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <div style="border-top:1px solid #bababa; margin:0">
    <?php echo get_avatar( get_the_author_email(), '80' ); ?>
    
    <h4 style="padding-top:10px;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br /><small><?php the_author_posts_link(); ?> </small></h4>
    		<?php
            the_excerpt();
    		?>
            <p class="more-link"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" style="color: rgb(157, 20, 20); font-weight: bold;"><?php _e('Read', 'news'); ?></a> | Comments (<?php comments_number('', '1', '%'); ?>)</p>
            </div>
     <?     endwhile;
        }
    
      }
    }
    ?>

    Thread Starter JC Palmes

    (@khleomix)

    Hi Michael,

    Thanks for the code but it does not seem to work… I am using the php code widget to add the code to. but its giving me an error:

    Fatal error: Call to a member function on a non-object in /home4/swarfblo/public_html/tmw/wp-content/plugins/php-code-widget/execphp.php(43) : eval()’d code on line 4

    MichaelH

    (@michaelh)

    After the first <?php add this line:

    global $wpdb;

    Thread Starter JC Palmes

    (@khleomix)

    thank you so much! you’re a lifesaver!

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Author list in sidebar’ is closed to new replies.