author list not linking to correct page

I have an odd problem with the author list. I am using the below code in my sidebar to pull in the gravatar and link to author’s profile. However, the links it’s actually generating are wrong. It just generates links to the page that it’s on. So when you’re on the blog page, all the author links are www.mysite.com/blog/. Any idea why this could be happening??

<?php
    $order = 'user_nicename';
    $user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE ID <> 1 ORDER BY $order"); // query users
    foreach($user_ids as $user_id) : // start authors' profile "loop"
    $user = get_userdata($user_id);
    ?>
    <li class="clearfix"><?php echo get_avatar( $user->ID, '80' ) . '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?></li>
    <?php
    endforeach; // end of authors' profile 'loop'

?>

Related posts

Leave a Reply

2 comments

  1. To generate a link to the author’s page/archive on your website, use get_author_posts_url with the author’s user ID.

    In your case:

    <li class="clearfix"><?php echo get_avatar( $user->ID, '80' ) . '<a href="' . get_author_posts_url( $user->ID ) . '">' . $user->display_name . '</a>'; ?></li>
    
  2. On the one hand, there’s the get_users() function Arguments @Codex that wraps up the user query class and should be used instead of a direct query.

    On the other hand – if you’re forced (as in: “someone is pointing a gun on my hand”) – then you should prepare() your query properly:

    $user_ids = $wpdb->get_col( $wpdb->prepare( 
         "SELECT ID FROM $wpdb->users WHERE ID <> 1 ORDER BY %s"
        ,$order
    ) );