I am using wp query results in a shortcode, and right now I am echoing out the stuff I need, or just using wp functions (title(), the_content() etc.).

The problem is, if I put that shortcode in a post, and I put it on the top and write some text below the shortcode, the shortcode result is ALWAYS at the bottom.

I know that is because I have to RETURN values from a shortode, but how do I return the following?

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        echo '<li>' . get_the_title() . '</li>';
    echo '</ul>';

I tried appending each function and html to a variable and returning that, but I don't get the same result as when I do the above.

  • 1
    See this post – Pieter Goosen Nov 18 '14 at 18:00
  • Thanks, seems to be working! the only problem I have now is that it doesn't work if the shortcode is placed inside a text widget ( or to be more specific, the js plugin in the shortcode does not get activated it seems). So, if I place the shortcode directly in a theme file (e.g. index.php) it works, if I place it inside a page/post it works, but in a text widget it does not work properly ( and it did before ). any idea? – Ollicca Mindstorm Nov 18 '14 at 18:56
  • Did you add support for shortcodes in text widgets. Please add your current code – Pieter Goosen Nov 18 '14 at 19:00
  • Yes. I added the ob start right after the start of the function and it works now with text widget as well! Thank you so much. – Ollicca Mindstorm Nov 18 '14 at 19:02
  • My pleasure, glad I could help. Enjoy :-) – Pieter Goosen Nov 18 '14 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.