Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 短代码只返回循环的第一个值_Wordpress_Shortcode - Fatal编程技术网

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 ;
     }