Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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'

?>

share|improve this question
    
$user->user_url will not point to the user's profile on your site (as I think you think it does). It's the link the author has provided by editing his profile. If he hasn't updated his profile there will be no URL in the database and browsers interpret <a href="">afkjl</a> as a link to the page that it's currently on. –  Calle Oct 22 '12 at 16:01
    
oh, thanks. is there any solution that can fix this to point to the author's url on my site? –  jamie Oct 22 '12 at 16:18
1  
I wrote it as an answer so that other's could find it. BTW if you are just interested in getting all user objects, you may want to use get_users instead of the wpdb class. –  Calle Oct 22 '12 at 16:28

2 Answers 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
) );
share|improve this answer

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>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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