WordPress.org

Ready to get started?Download WordPress

Forums

use return instead of echo in shortcode function (6 posts)

  1. antistandard
    Member
    Posted 2 years ago #

    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]

  2. brasofilo
    Member
    Posted 2 years ago #

    can you post the code again?
    (as per forumRules, of course..:)

  3. antistandard
    Member
    Posted 2 years ago #

    Sure, sorry that is new to me.

    Code:

    http://pastebin.com/eRmCBLyQ

    Thanks

  4. brasofilo
    Member
    Posted 2 years ago #

    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

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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.

  6. antistandard
    Member
    Posted 2 years ago #

    @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.

Topic Closed

This topic has been closed to new replies.

About this Topic