If the post does not have a manual excerpt (as in one added to the Excerpt box on the post edit screen) then get_the_excerpt()
used wp_trim_excerpt()
to generate an excerpt. The problem is that wp_trim_excerpt()
uses the content of the global $post
object, which is set in the loop, regardless of any ID passed to get_the_excerpt()
.
So to use get_the_excerpt()
outside the loop you need to use setup_postdata()
to set the global $post
to the desired post:
<?php
$blogPost1 = wp_get_recent_posts( array(
'numberposts' => 1,
'category' => 42,
'orderby' => 'post_date',
'order' => 'DESC'
) );
global $post;
$post = $blogPost1[0]; // Post must be assigned to the global $post variable.
setup_postdata( $post );
?>
<!-- Now you can use template tags without specifying the ID. -->
<h1><?php the_title(); ?></h1>
<br>
<?php the_excerpt(); ?>
<br>
<?php wp_reset_postdata(); ?>
But, based on the content you’ve provided, you’re using the <!--more-->
tag, so you don’t actually want the excerpt. To show only content above the more tag you need to use the_content()
. the_content()
works so that the content above the more tag is displayed only on non-singular pages, while the full text is displayed on single pages. Note that this behaviour is regardless of whether the_content()
is in the main query or not. To force it to only show the content above the more tag you need to use the global $more
variable:
setup_postdata( $post );
global $more;
$original_more = $more;
$more = 0;
?>
<h1><?php the_title(); ?></h1>
<br>
<?php the_content(); ?>
<br>
<?php
wp_reset_postdata();
$more = $original_more;