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 this loop in my wordpress site which displays the latest posts with their titles, I want to display the post content after the title. I normally retrieve it with the function get_the_content but I cannot make it work in this case. This is the loop:

   while ( $q_query->have_posts() )
   {

    $q_query->next_post();
    $question = get_post($q_query->post);
   $loophtml = $loophtml . "<li><span class='list-question-title'>" . "<a class='list-answer-link' href='" . get_permalink($question->ID) ."'>" . $question->post_title . "</a></span>";
   $loophtml = $loophtml . "<span class='list-number-answers'>" . get_comments_number($question->ID) . " comentarios</span>&nbsp;&#183;&nbsp<a href='" . get_permalink($question->ID) ."'>Comentar</a>";
   $loophtml = $loophtml . "</li>";
   }

Anyone knows how I could do it? Thanks

share|improve this question

2 Answers 2

up vote 3 down vote accepted

try this instead:

...

global $post;
while ($q_query->have_posts()){
  $q_query->the_post();

  $loophtml .= "<li><span class='list-question-title'>" . "<a class='list-answer-link' href='" . get_permalink() ."'>" . get_the_title() . "</a></span>";
  $loophtml .= get_the_content();
  $loophtml .= "<span class='list-number-answers'>" . get_comments_number() . " comentarios</span>&nbsp;&#183;&nbsp<a href='" . get_permalink() ."'>Comentar</a>";
  $loophtml .= "</li>";
}
wp_reset_query();

...
share|improve this answer
    
Thanks that worked –  Lisandro Dec 18 '10 at 22:15

A little unusual Loop structure you have there. get_the_content() works inside the Loop and with global variables set up. And you are not setting up those global variables.

To work with your current code it will be something like:

apply_filters('the_content', $question->post_content);

To make template tags work properly you need to use setup_postdata(), see examples in get_posts() documentation.

share|improve this answer
2  
Or.. you should also be able call the_post() to setup the vars.. ie. $q_query->the_post(); ... it should have exactly the same effect.. –  t31os Dec 18 '10 at 20:43

Your Answer

 
discard

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