在WordPress的MySQL查询中获取作者的头像

时间:2015-12-15 06:43:59

标签: wordpress

我正在获取作者ID,使用我的WordPress网站中的以下代码从wp_cp表中获取点,并且它完美地运行。我现在正在尝试的是在用户ID旁边获取用户头像。如何在代码中获取用户头像?

<?php

  global $wpdb;
  /* wpdb class should not be called directly.global $wpdb variable is an    instantiation of the class already set up to talk to the WordPress database */ 

  $result = $wpdb->get_results( "SELECT uid,sum(points) as pt FROM wp_cp   where timestamp between '2015-12-12' and '2015-12-31' group by uid  "); /*mulitple row results can be pulled from the database with get_results function and outputs an object which is stored in $result */

  //echo "<pre>"; print_r($result); echo "</pre>";
  /* If you require you may print and view the contents of $result object */

  echo "uid"."  "."pt"."<br><br>";



  foreach($result as $row)
  {

  echo $row->uid."  ".$row->pt."<br>";

  }
  /* Print the contents of $result looping through each row returned in the result */

 ?> 

2 个答案:

答案 0 :(得分:0)

如果您使用uid字段(与wp_users中的用户ID匹配),请将其传递给get_avatar函数:

https://codex.wordpress.org/Function_Reference/get_avatar

答案 1 :(得分:0)

添加以下代码以获取foreach循环内的用户头像。

echo get_avatar( $row->uid );

由于您要求尺寸,您可以为此功能添加参数。

echo get_avatar( $row->uid, 64 ); // So the avatar size will be 64px X 64 px.

有关详细信息,请查看此URL