• Zhen

    (@milohuang)


    Greeting all,

    I am trying to insert custom field with name of “Screenshot” (there are several of them) to the shortcode as below:

    $mykey_values = get_post_custom_values('Screenshot');
      foreach ( $mykey_values as $key => $value ) {
        echo '<img src="'.$value.'" alt="Screenshot" />';
      }

    But when you use echo in shortcode, it will be placed in the wrong position (top of the post). According to WP Codex, you will have to use return and I am wondering how I can change the code above to return all my custom fields with the name of “Screenshot” to the shortcode. Thank you very much.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Michael

    (@alchymyth)

    for example:

    $mykey_values = get_post_custom_values('Screenshot');
    $output = '';
      foreach ( $mykey_values as $key => $value ) {
        $output .= '<img src="'.$value.'" alt="Screenshot" />';
    return $output;
      }

    (untested)

    Thread Starter Zhen

    (@milohuang)

    Does not seem to work but anyway, I think I found a way to implement it using conditional get_post_custom_values to output all the necessary custom fields. Thank you very much anyway!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to change echo to return in shortcode’ is closed to new replies.