0

I need some help I have the iteration working for listing the wordpress users however I also want to wrap their names in links and can't seem to get the user url to work... Nothing is returned to me.

Here is what I have so far.... You will see that I am trying to pull in the user_url however it just returns as nothing. Any help would be great! Thanks

<?php
  $blogusers = get_users( 
    array( 
      'role' => 'subscriber',
      'fields' => 'all' 
    ) 
  );
  echo '<ul>'; 
  foreach ( $blogusers as $user ) {
    echo '<li><a href="' . $user->user_url  . '">' . $user->display_name . '</a>';
  }
  echo '</ul>'
?>
2
  • Have you considered using WP_User_Query?
    – Tom J Nowell
    Commented Mar 29, 2015 at 23:41
  • I'll give that a go... was hoping get_users could do the trick though.
    – bjrdesign
    Commented Mar 29, 2015 at 23:51

2 Answers 2

1

You could take a look at the contributors function included in Twenty Fourteen

if ( ! function_exists( 'twentyfourteen_list_authors' ) ) :
/**
 * Print a list of all site contributors who published at least one post.
 *
 * @since Twenty Fourteen 1.0
 */
function twentyfourteen_list_authors() {
    $contributor_ids = get_users( array(
        'fields'  => 'ID',
        'orderby' => 'post_count',
        'order'   => 'DESC',
        'who'     => 'authors',
    ) );

    foreach ( $contributor_ids as $contributor_id ) :
        $post_count = count_user_posts( $contributor_id );

        // Move on if user has not published a post (yet).
        if ( ! $post_count ) {
            continue;
        }
    ?>

    <div class="contributor">
        <div class="contributor-info">
            <div class="contributor-avatar"><?php echo get_avatar( $contributor_id, 132 ); ?></div>
            <div class="contributor-summary">
                <h2 class="contributor-name"><?php echo get_the_author_meta( 'display_name', $contributor_id ); ?></h2>
                <p class="contributor-bio">
                    <?php echo get_the_author_meta( 'description', $contributor_id ); ?>
                </p>
                <a class="button contributor-posts-link" href="<?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?>">
                    <?php printf( _n( '%d Article', '%d Articles', $post_count, 'twentyfourteen' ), $post_count ); ?>
                </a>
            </div><!-- .contributor-summary -->
        </div><!-- .contributor-info -->
    </div><!-- .contributor -->

    <?php
    endforeach;
}
endif;

You could also change the get_users function to WP_User_Query if you need to use other parameters.

3
  • Can you edit your answer to explain how it helps? Currently it's the same as a where's wally game, the OP doesn't know what to look for
    – Tom J Nowell
    Commented Mar 30, 2015 at 10:12
  • Already included in the code above <?php echo esc_url( get_author_posts_url( $contributor_id ) ); ?> Commented Mar 31, 2015 at 6:34
  • That's the link to the users author archive, not the users URL that they set in their profile
    – Tom J Nowell
    Commented Mar 31, 2015 at 14:15
-1

Just needed to use get_author_posts_url($user->ID)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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