Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Currently I'm using this shortcode within a post

http://pastebin.com/K7WC2Lk3

which works great, but somehow I can't add any text in front of this shortcode, without posting it the other way around.

In example

some dummy text [etalage]

will always be shown as

[etalage] some dummy text

I've read to gather the value into a variable and return it, but I have to admit, I'm not entirely sure how to accomplish this best

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

Here's your updated shortcode function which puts output in a variable and returns it. Note that you have to use functions that return data rather than output directly, in this case get_the_post_thumbnail() is used in place of the_post_thumbnail()

function cor_etalage_add_shortcode() {
    $output = '<div id="etalage"><ul>';

    $the_query = new WP_Query(array(
        'post_type' => array(
            'portfolio'
        )
    ));

    while ($the_query->have_posts()) : $the_query->the_post();
        $output .= '<li>'. get_the_post_thumbnail($post->ID,'etalage') .'</li>';
    endwhile;
    wp_reset_postdata();
    $output .= '</ul></div>';

    return $output;
}
add_shortcode('etalage', 'cor_etalage_add_shortcode');

EDIT oops, forgot to pass the post ID with get_the_post_thumbnail, updated.

share|improve this answer
    
Thanks, Milo, It completely answered my question and more! (it seems I can't edit your reply, but I've added an extra $ before output .= '</ul></div>';) –  Cor van Noorloos Jun 6 '11 at 20:07
    
@Cor van Noorloos - ah, thanks, missed that! I've updated the answer to fix it. –  Milo Jun 6 '11 at 20:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.