0

the shortcode given below returns only the first value:

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');

However if I use "echo" it gives all the values. What am I missing here?

1 Answer 1

1

In foreach ,store all the value in some variable and then return that variable after foreach loop

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

add_shortcode('show_completed_quiz', 'completed_quiz');

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.