List Authors For Current Category

I want to list all unique authors that have posts in the current category.

My code (location: wp-content/themes/custom-theme/category.php)

div class="container"
    div class="row"

      ?php if ( have_posts() ) : ?

        ?php while( have_posts() ) : the_author(); ?

            div class="col-md-2 text-center"
                ?php the_author_meta( 'display_name', 30 ); ?
            /div

        ?php endwhile; ?

      ?php else: ?

        p class="text-center"No authors/p

      ?php endif; ?

     /div
/div

This is not giving expected result, can someone help me?

Topic list-authors loop categories posts Wordpress

Category Web


Execution of code goes to infinite loop because you missed the_post() in loop.

You can list all authors from current category:-

  • Create an empty array for author IDs
  • Check if current author ID exist in that array
  • If No, display the author name and store that author ID in array.
  • If Yes, Then skip that post and move to next.

Consider this example:-

<div class="container">
    <div class="row"><?php 

        if ( have_posts() ) { 
            $unique_authors = array();

            while( have_posts() ) { 
                the_post(); ?>

                <div class="col-md-2 text-center"><?php
                    if (!in_array(get_the_author_meta('ID'), $unique_authors)) {
                        the_author_meta( 'display_name' );
                        array_push($unique_authors, get_the_author_meta('ID'));
                    } ?>
                </div><?php 

            }
        } else { ?>
            <p class="text-center">No authors</p><?php
        } ?>

     </div>
</div>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.