Error in return of database users

Asked

Viewed 19 times

0

[RESOLVED] I am selecting the compos id, user_nicename and display_name of the database, in this I create a foreach to show the result, but the right one would return me all users but returns the number of registered users, repeating the same user

<?php
global $wpdb;
$authors = $wpdb->get_results("SELECT ID, user_nicename from $wpdb->users ORDER BY display_name");
    foreach ($authors as $authors ) {
        $aid = $authors->ID;
        ?>
            <div class="autor">
                <div class="avatar"><?php $email = get_the_author_email(); $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5($email). "&default=".urlencode($GLOBALS['defaultgravatar'] ); $usegravatar = get_option('woo_gravatar');?><img src="<?php echo $grav_url; ?>" alt="" width="80" height="80" /></div>
                <div class="autorcontent">
                    <div class="nomeautor"><a href = "<?php the_author_url ();?>" itemprop="url"><?php the_author(); ?></a></div>
                </div>
                <div class="redesautor">
                    <div class="autorredes"><a href="<?php the_author_meta( 'instagramuser' ); ?>?rel=author" target="_blank"></a>
                    <?php if ( get_the_author_meta( 'instagramuser' ) ) { ?><a href="<?php the_author_meta( 'instagramuser' ); ?>?rel=author" title="siga o perfil no instagram" target="_blank"><i class="fab fa-instagram"></i></a><?php } ?></div>
                    <div class="autorredes"><a href="<?php the_author_meta( 'twitteruser' ); ?>" target="_blank"></a>
                    <?php if ( get_the_author_meta( 'twitteruser' ) ) { ?><a href="<?php the_author_meta( 'twitteruser' ); ?>" title="suia o perfil no Twitter" target="_parent"><i class="fab fa-twitter-square"></i></a><?php } ?></div>
                    <div class="autorredes"><a href="<?php the_author_meta( 'facebookuser' ); ?>" target="_blank"></a>
                    <?php if ( get_the_author_meta( 'facebookuser' ) ) { ?><a href="<?php the_author_meta( 'facebookuser' ); ?>" title="siga o perfil no Facebook" target="_blank"><i class="fab fa-facebook-square"></i></a><?php } ?></div>
                </div>
            </div>
        <?php } ?>

result image: inserir a descrição da imagem aqui

  • I haven’t dealt with PHP for a long time, but I wonder if the way you are passing the table in SELECT is actually working...

1 answer

0

I decided as follows:

<?php
 $display_admins = false;
 $order_by = 'post_count'; // 'nicename', 'email', 'url', 'registered', 'display_name', or 'post_count'
 $order = 'DESC';
 $role = ''; // 'subscriber', 'contributor', 'editor', 'author' - leave blank for 'all'
 $avatar_size = 161;
 $hide_empty = false; // hides authors with zero posts

 if(!empty($display_admins)) {
      $blogusers = get_users('orderby='.$order_by.'&role='.$role);
 } else {

 $admins = get_users('role=administrator');
 $exclude = array();

 foreach($admins as $ad) {
      $exclude[] = $ad->ID;
 }

 $exclude = implode(',', $exclude);
 $blogusers = get_users('exclude='.$exclude.'&orderby='.$order_by.'&order='.$order.'&role='.$role);
 }

 $authors = array();
 foreach ($blogusers as $bloguser) {
 $user = get_userdata($bloguser->ID);

 if(!empty($hide_empty)) {
      $numposts = count_user_posts($user->ID);
      if($numposts < 1) continue;
      }
      $authors[] = (array) $user;
 }

 echo '<ul id="grid-contributors">';
 foreach($authors as $author) {
      $display_name = $author['data']->display_name;
      $avatar = get_avatar($author['ID'], $avatar_size);
      $author_profile_url = get_author_posts_url($author['ID']);

      echo '<li class="single-item">';
      echo '<div class="author-gravatar"><a href="', $author_profile_url, '">', $avatar , '</a></div>';
      echo '<div class="author-name"><a href="', $author_profile_url, '" class="contributor-link">', $display_name, '</a></div>';
      echo '</li>';
      }
 echo '</ul>';

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.