Wordpress 短代码只返回循环的第一个值
下面给出的短代码仅返回第一个值:Wordpress 短代码只返回循环的第一个值,wordpress,shortcode,Wordpress,Shortcode,下面给出的短代码仅返回第一个值: function completed_quiz(){ global $wpdb; $current_user = wp_get_current_user(); $userID = $current_user->ID; $fetch22 = $wpdb->get_results("MySQL Query"); foreach($fetch22 as $item){ return "Quiz ID: $it
function completed_quiz(){
global $wpdb;
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$fetch22 = $wpdb->get_results("MySQL Query");
foreach($fetch22 as $item){
return "Quiz ID: $item->quiz_id Percentage: $item->result <br>";
}
}
add_shortcode('show_completed_quiz', 'completed_quiz');
函数已完成{
全球$wpdb;
$current_user=wp_get_current_user();
$userID=$current\u user->ID;
$fetch22=$wpdb->get_结果(“MySQL查询”);
foreach($fetch22作为$item){
返回“测验ID:$item->测验ID百分比:$item->结果
”;
}
}
添加快捷码(“显示完成的测验”、“完成的测验”);
但是,如果我使用“echo”,它会给出所有值。我缺少什么?在foreach中,将所有值存储在某个变量中,然后在foreach循环后返回该变量函数已完成{
全局$wpdb;
$current_user=wp_get_current_user();
$userID=$current\u user->ID;
$fetch22=$wpdb->get_结果(“MySQL查询”);
$returnVal='';
foreach($fetch22作为$item){
$returnVal.=“测验ID:$item->测验ID百分比:$item->result
”;
}
return$returnVal;
}
添加快捷码(“显示完成的测验”、“完成的测验”)
function completed_quiz(){
global $wpdb;<br/>
$current_user = wp_get_current_user();
$userID = $current_user->ID;
$fetch22 = $wpdb->get_results("MySQL Query");
$returnVal ='';
foreach($fetch22 as $item){
$returnVal .= "Quiz ID: $item->quiz_id Percentage: $item->result <br>";
}
return $returnVal ;
}