Support » Fixing WordPress » use return instead of echo in shortcode function

  • Hello,

    I have a function that is called by a shortcode that I made and it works well.

    It displays all the images that I attach to a post.

    I use echo to show the results but the outputted images don’t show up in the correct place in the post.

    I know that I am meant to use ‘return’ instead of echo, but return only returns 1 image not all the images attached to the post.

    I presume that I am meant to make the images into a string to then return, but I am bad at php and don’t know how to do it.

    Can someone please show me so I can remember and then use that new knowledge as I continue to learn php and wordpress dev.

    Thank you

    [Code moderated as per the Forum Rules. Please use the pastebin]

Viewing 5 replies - 1 through 5 (of 5 total)
  • can you post the code again?
    (as per forumRules, of course..:)

    Sure, sorry that is new to me.

    Code:

    http://pastebin.com/eRmCBLyQ

    Thanks

    when i saw your code, the first thing i noticed was: where does the $qty var is getting its value?

    you’re missing the attributes in your function, check this documentation and use the extract attributes as in the first example

    Michael

    (@alchymyth)

    Forum Moderator

    I presume that I am meant to make the images into a string to then return, but I am bad at php and don’t know how to do it.

    yes – that is basically how it could be done.

    http://pastebin.com/tCPdKpRm

    and please do look into the questions by @brasofilo.

    @brasofilo – this is only a snipet of code that I had first made to work in a template php file. Then I wanted to see if I could make it into a shortcode function and that is why it looks like it does not reference anything, but when the shortcode gets triggered, this shortcode block relates to other code inside the post/page that it sits in and seems to work fine. Thank you for that inquiry.

    @alchymyth – I seem to remember you helping me once before. Thank you very much for your fix. IT WORKS! and now I can learn from you how to make that array output. Thanks again for your help and accuracy.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘use return instead of echo in shortcode function’ is closed to new replies.