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.

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();
       endforeach;
       return $mypost;

   } 
share|improve this question
2  
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
add comment

2 Answers

up vote 1 down vote accepted

codex the_title()

replace this

$mypost .= the_title(); 

with

$mypost .= the_title('','',false); 
share|improve this answer
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
add comment

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
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.