WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a page, with a shortcode that get all the post from a categorie and put after the content of the page. For no apparent reason, the result of a WP_Query() in the shortcode APPEAR ALWAYS at the top of the page. If i put the shortcode a the top OR at the bottom, the post (3 of them) appear before the content of the page...

on that one, i have NO IDEA why... shortcode is at the en of the page

after the comment i post the code the ECHO... this is the problem ?.. i to solve it ?

//Add a ShorCode to get a page/post content
add_shortcode ('post_cat2','get_post_cat2');
   function get_post_cat2 ($att) {

       $mypost = array();

       $args = array( 'category' => 36 );
       $myposts = get_posts( $args );

       foreach( $myposts as $post ) :   setup_postdata($post);
       $mypost .= the_title();
       return $mypost;

share|improve this question
You're probably echoing the content. Try returning it. – Rutwick Gangurde Mar 13 '12 at 4:49
yep i echo, can you explain more ? – menardmam Mar 13 '12 at 5:09
up vote 1 down vote accepted

codex the_title()

replace this

$mypost .= the_title(); 


$mypost .= the_title('','',false); 
share|improve this answer
+1. Or use get_the_title(). Either way: the issue is that the_title() echoes rather than returns its output, causing the shortcode output to print prematurely. – Chip Bennett Mar 13 '12 at 14:56

I guessed it! Read the first paragraph of this. Try it. I'm sure it will solve your problem.

share|improve this answer
ok now the question with something like that : $query = new WP_Query( 'p=1157' ); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); the_content(); – menardmam Mar 13 '12 at 5:34
the_content(); echo... hot to return a value...??????? – menardmam Mar 13 '12 at 5:35
return get_the_content(). – Rutwick Gangurde Mar 13 '12 at 5:38

Your Answer


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.