Foreach 循环仅使用 return 执行第一个值,但 returns 使用回显的所有值

Foreach loop executes only first value with return, but returns all values with echo

我正在尝试使用 affiliatewp 创建联盟排行榜。我能够获得我想要的每个值,但是,在我使用 foreach 循环的函数中,它调用每个分支机构并显示月份的 "sales"。

function affiliate_leaderboard_function() {
global $wpdb;

$getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
$getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
$current_month = get_the_date("m");
$current_year = get_the_date("Y");
$current_date = get_the_date("Y-m-d");
$lastday = date('t',strtotime($current_date));

foreach ($getallaffiliates as $theaffiliate) {
    $user_id = get_userdata( $theaffiliate->user_id );
    $userfirstname = $user_id->first_name;
    $userlastname = $user_id->last_name;
    $totalreferrals = $theaffiliate->referrals;
    $affiliate_id = $theaffiliate->affiliate_id;
    $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
    $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
    if(!empty($getaffreferrals)){
        echo $closerstring;
    }
}
}
add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');

因此,当我将短代码放置到位并使用 "echo $closerstring" 时,它会吐出所有正确的数据,用户的名字后跟他们当月的销售额。但是简码输出在内容的顶部。

当我将其切换为 "return $closerstring" 时,foreach 循环中只有 returns 一个附属机构,而不是所有附属机构。我不知道如何让它像 echo 函数那样显示所有值,但我需要它出现在正确的位置...

Return中断函数的执行。

你需要将输出保存在一个字符串中,return它在最后,像这样:

[...]
    $output= '';
    foreach ($getallaffiliates as $theaffiliate) {
        [...]
        if(!empty($getaffreferrals)){
            $output .= $closerstring;
        }
    }
    return $output;
}

在每个循环之后构建您的字符串,而不是 echo 然后 return 最后:

$result = ""; // Create an empty string

foreach ($getallaffiliates as $theaffiliate) {
  $user_id = get_userdata( $theaffiliate->user_id );
  $userfirstname = $user_id->first_name;
  $userlastname = $user_id->last_name;
  $totalreferrals = $theaffiliate->referrals;
  $affiliate_id = $theaffiliate->affiliate_id;
  $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
  $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
  if(!empty($getaffreferrals)){
    $result .= $closerstring; // Add the data
  }
}

return $result; // you return the full string

在 for 循环中,如果你写 return,那么循环会在第一个 运行 处退出。相反,使用变量将代码更改为

$return = '';
foreach ($getallaffiliates as $theaffiliate) {
$user_id = get_userdata( $theaffiliate->user_id );
$userfirstname = $user_id->first_name;
$userlastname = $user_id->last_name;
$totalreferrals = $theaffiliate->referrals;
$affiliate_id = $theaffiliate->affiliate_id;
$getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
$closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
if(!empty($getaffreferrals)){
    $return_array.= $closerstring;
}
}

return $return_array;

附加字符串和return。

    function affiliate_leaderboard_function() {
    global $wpdb;

        $getallreferrals = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_referrals`");
        $getallaffiliates = $wpdb->get_results( "SELECT * FROM `wp_affiliate_wp_affiliates`");
        $current_month = get_the_date("m");
        $current_year = get_the_date("Y");
        $current_date = get_the_date("Y-m-d");
        $lastday = date('t',strtotime($current_date));

        $returnstring='';
        foreach ($ge1tallaffiliates as $theaffiliate) {
          $user_id = get_userdata( $theaffiliate->user_id );
          $userfirstname = $user_id->first_name;
          $userlastname = $user_id->last_name;
          $totalreferrals = $theaffiliate->referrals;
          $affiliate_id = $theaffiliate->affiliate_id;
          $getaffreferrals = $wpdb->get_results( "SELECT `date` FROM `wp_affiliate_wp_referrals` WHERE `affiliate_id` = $affiliate_id AND `date` >= '$current_year-$current_month-01:00:00:00' AND `date` < '$current_year-$current_month-$lastday:23:59:59' ORDER BY `wp_affiliate_wp_referrals`.`referral_id` ASC");//Get all referrals by affiliate id
       $closerstring = $userfirstname." | This Month's Sales: ".count($getaffreferrals)."<br>";
        if(!empty($getaffreferrals)){
            $returnstring .=$closerstring;
           }
       }
           return $returnstring;
    }
    add_shortcode('affiliate_leaderboard' , 'affiliate_leaderboard_function');