-1

I have this code that works perfect:

<?php
$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('posts_per_page=5' . '&paged='.$paged);  
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<span><?php the_date('m/d/y'); ?></span>

<a href="<?php the_permalink(); ?>"><button>Read More</button></a>

<?php endwhile; ?>

I want to display an excerpt of the post content between the date and button. So I added <p><?php the_content(); ?></p>. Didn't work. I tried <p><?php get_the_content(); ?></p>. Still nothing.

I tried putting the <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> loop around all of the HTML and just the paragraph. Both yielded nothing.

How do I get the excerpt to display?

UPDATE

<?php
$my_query = new WP_Query( array( 'posts_per_page' => 5 ) );
if ($my_query->have_posts()) :
while ($my_query->have_posts()) : $my_query->the_post(); ?>

<h2><?php the_title(); ?></h2>
<span><?php the_date('m/d/y'); ?></span>
<p><?php the_content(); ?></p>
<a href="<?php the_permalink(); ?>"><button>Read More</button></a>

<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
  • Is your WordPress installation set up for debugging? If so, add error_log( print_r( $my_query, true ) ); inside the loop. This will dump the value of $my_query into the log for each iteration through the while loop. On the surface your code looks correct to me. – Cedon Jan 15 '18 at 22:29
  • @Cedon set up debugging and added the above statement into the loop. I got a bunch of stuff in the log that I don't know how to even begin to understand. – jrial Jan 19 '18 at 3:38
  • Can you edit your question to include the output of one iteration in a code block? – Cedon Jan 19 '18 at 22:28
  • The problem was resolved. Somehow the content of the post was deleted. So it was working correctly but there was no content to display. – jrial Jan 21 '18 at 0:20
-1

Your query is structured a little weird.

$my_query = new WP_Query( array( 'posts_per_page' => 5 ) );

if ( $my_query->have_posts() ) {
   while ( $my_query->have_posts() ) {
      $the_query->the_post();

      // Your desired template code

   } // Close while()

   // Restore original Post Data
   wp_reset_postdata();

} // Close if()

This should be all you need to set it up since WP_Query pages by default unless you override it by setting 'nopaging' => true in your arguments for it.

Note that after the while() loop ends, the statement wp_reset_postdata(); is used. This resets the original post data to the defaults.

  • Awesome, thanks! I made the suggested adjustments but the_content() still is not providing anything. – jrial Jan 13 '18 at 18:52
  • Can you update your question with the full code you're trying including where you're calling the_content()? – Cedon Jan 13 '18 at 19:36
  • Question has been updated – jrial Jan 15 '18 at 19:22

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.