0

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?

  • 1
    Do you have a user with ID of 30? – denis.stoyanov Mar 11 '16 at 22:16
  • Yes, that was just a test. I don't need that, I need to display all authors in that category. I am getting back something similar to infinite loop... Without names... – Morpheus Mar 11 '16 at 22:40
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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