WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Show author only once? (9 posts)

  1. Doodlebee
    Member
    Posted 5 years ago #

    Okay - gonna try and ask a question again...

    I'm trying to make a list of authors show up in my sidebar. Basically in the main content there will be a list of posts. In the sidebar, I want a duplicate loop but ONLY with the list of author names whose posts are displayed on that particular listing page.

    For example, say you have (in your settings) to display 5 posts per listing page. For example, say these 5 posts were written (in order) by admin, Richard, Richard, Mark and admin. In the sidebar, I put in a function that basically did a second loop, but had only the author names listed. However, it's listing the names multiple times, not just once:

    admin
    Richard
    Richard
    Mark
    admin

    What I want is :

    admin
    Richard
    Mark

    I'm really close to having this work, but I'm stuck on one final bit, and I was hoping someone could take a peek for me. Right now, I have the following code:

    $name = '';
    	$postlimit = get_option('posts_per_page');
    	$query = new WP_Query('showposts=' . $postlimit);
    	if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
    	  $author = get_the_author();
    	  if($author != $name) {
    	    $name = $author;
    	    $count = '1';
    	  }
    	  if($count == '1') {
    	    get_my_image('avatar'); // this is another custom function
    	    echo $name . '<br />';
    	  }
    	  $count++;
    	endwhile;
    	endif;

    It's REALLY close, but the above lists:

    admin
    Richard
    Mark
    admin

    When I echo out the count, the final "admin" (the second one that shows up) is given a count of "1". It *should* be giving it "2", and thus not displaying it. Would anyone know what I could do to fix this? Any help would be appreciated.

  2. Dagon Design
    Member
    Posted 5 years ago #

    You could create an array, and for each author, see if they exist in the array already. If so, skip them. If not, add them to the array, and display it. Then you can also get rid of the current code you have in there thats comparing against the last author.

    So something like this:

    $postlimit = get_option('posts_per_page');
    $query = new WP_Query('showposts=' . $postlimit);
    $author_list = array();
    if($query->have_posts()) : while($query->have_posts()) : $query->the_post();
      $author = get_the_author();
      if (in_array($author, $author_list) == FALSE) {
        get_my_image('avatar'); // this is another custom function
        echo $name . '<br />';
        $author_list[] = $author;
      }
      $count++;
    endwhile;
    endif;
  3. Joni
    Member
    Posted 5 years ago #

    Did you ever find a solution? I think I may need something similar here in the next few weeks.

  4. Doodlebee
    Member
    Posted 5 years ago #

    Yep - here it is:

    $root = $_SERVER['DOCUMENT_ROOT'];
    $path = get_bloginfo('home');
    function list_page_authors() {
    	global $post, $root, $path;
    
    	$output = array();
    	if(have_posts()) : while(have_posts()): the_post();
    	  $id = get_the_author_meta('ID');
    	  $id_arr[] = $id;
    	endwhile; endif;
    
        $clean = array_unique($id_arr);
        $count = '1';
    
        foreach($clean as $id) {
          $first = get_the_author_meta('first_name', $id);
          $last = get_the_author_meta('last_name', $id);
          $name = $first . ' ' . $last;
          $position = get_the_author_meta('position', $id);
          $description = get_the_author_meta('description', $id);
          $url = get_the_author_meta('user_url', $id);
          $img = '/wp-content/gravatars/' . $first . '_' . $last . '.jpg';
    
          if(file_exists($root . $img)) {
    	    $myimg  = '<img class="avatar" src="' . $path . $img . '" alt="' . $name . '" />';
          } else {
            $myimg = get_avatar( $post );
          } ?>
    
          <div class="authorinfo<?php if($count == '1') { echo ' first';} ?>">
        	<?php echo $myimg; ?>
        	<h5><?php echo $name;?></h5>
        	<h6><?php echo $position; ?></h6>
        	<p><?php echo $description; ?></p>
        	<?php if($url != '') { ?><a class="more-link" href="<?php echo $url; ?>">read more</a><?php } ?>
          </div>
    <?php $count ++;
          }
    }
  5. Joni
    Member
    Posted 5 years ago #

    Hmm. Topic is now shown as "resolved" and there's no resolution posted. Weirdorama!

  6. Doodlebee
    Member
    Posted 5 years ago #

    That *is* weird - my post is right above yours...(I can see it)

    How bizarre.

  7. Dagon Design
    Member
    Posted 5 years ago #

    I think my method above is a bit shorter than that ;) But glad it works for you!

  8. Doodlebee
    Member
    Posted 4 years ago #

    prob'ly so :) But I also had to use this for other things as well - as it turned out, it's developed even further than this, and is way more complicated because of what I needed it for. Instead of boosting this one again, I'm going to be posting about the whole thing on my site - maybe making a plugin of my own on it.

    *believe me* - I looked at yours ;) I didn't want to code it all about if I didn't have to - but the requirements of the project made it necessary!

  9. mayuxi
    Member
    Posted 4 years ago #

    Here is my solution from sidebar:

    <ul>
    	<?php query_posts('showposts=50&tag=kolonki');
    	global $last_author, $posts_limit; $last_author = ''; $posts_limit=4;
    	while (have_posts()) : the_post(); global $last_author; if($post->post_author == $last_author) continue;
    	?>
    	<li>
    	<?php echo get_avatar( get_the_author_id(), 100); ?>
    	<em><?php the_author(); ?></em>
    	<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title (); ?></a></h3>
    	</li>
    	<?php global $post, $last_author, $posts_limit; if($posts_limit-- <=0) break; $last_author = $post->post_author; ?>
    	<?php endwhile; ?>
    </ul>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.