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;

  • 3
    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

codex the_title()

replace this

$mypost .= the_title(); 


$mypost .= the_title('','',false); 
  • 1
    +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.

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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