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.

Possible Duplicate:
Shortcode always displaying at the top of the page

I've developed a couple plugins which grab CPT items through a shortcode (via WP_Query). The problem I'm having is that any content entered in the WP Editor before I call the shortcode is run is always output after the shortcode data.

Editor:

My regular content here
[shortcode]
more content here

Page output:

shortcode data
My regular content here
more content here

Here's my shortcode query

$defaults = apply_filters( 'arconix_faq_shortcode_query_args',
    array(
    'post_type' => 'faq',
            'showposts' => 'all',
    'order' => 'ASC',
    'orderby' => 'title'
    )
);

extract( shortcode_atts( $defaults, $atts ) );

    /** Translate 'all' to -1 for query terms */
    if( $showposts == "all" ) $showposts = "-1";

    /** Create a new query bsaed on our own arguments */
$faq_query = new WP_Query( array( 'post_type' => $post_type, 'order' => $order, 'orderby' => $orderby, 'posts_per_page' => $showposts ) );

    if( $faq_query->have_posts() ) : while ( $faq_query->have_posts() ) : $faq_query->the_post();

    echo '<div id="post-' . get_the_ID() .'" class="arconix-faq-wrap">';
    echo '<div class="arconix-faq-title">' . get_the_title() . '</div>';
        echo '<div class="arconix-faq-content">' . get_the_content() . '</div>';
        echo '</div>';

    endwhile; endif; wp_reset_postdata();
}

Ideas?

share|improve this question

marked as duplicate by toscho Jun 5 '12 at 5:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer

up vote 1 down vote accepted

Your shortcode should return it's output and not echo it out ex:

$defaults = apply_filters( 'arconix_faq_shortcode_query_args',
    array(
    'post_type' => 'faq',
            'showposts' => 'all',
    'order' => 'ASC',
    'orderby' => 'title'
    )
);

extract( shortcode_atts( $defaults, $atts ) );

    /** Translate 'all' to -1 for query terms */
    if( $showposts == "all" ) $showposts = "-1";

    /** Create a new query bsaed on our own arguments */
    $faq_query = new WP_Query( array( 'post_type' => $post_type, 'order' => $order, 'orderby' => $orderby, 'posts_per_page' => $showposts ) );
    $RetVal = '';
    if( $faq_query->have_posts() ) : while ( $faq_query->have_posts() ) : $faq_query->the_post();

    $RetVal .= '<div id="post-' . get_the_ID() .'" class="arconix-faq-wrap">';
    $RetVal .= '<div class="arconix-faq-title">' . get_the_title() . '</div>';
    $RetVal .= '<div class="arconix-faq-content">' . get_the_content() . '</div>';
    $RetVal .= '</div>';

    endwhile; endif; wp_reset_postdata();
}
return $RetVal;
share|improve this answer
add comment

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