0

I am trying to get the excerpt of a blog post using the get_the_excerpt and it just returns blank every time and i cant work out what i am doing wrong. Here it was i got:

Template:

<div class="col-sm-12">
                    <?php $blogPost1 = wp_get_recent_posts( array(
                                                                'numberposts' => 1,
                                                                'category'    => 42,
                                                                'orderby'     => 'post_date',
                                                                'order'       => 'DESC'
                                                            ) ); ?>

                    <h1><?php echo $blogPost1[0]['post_title']; ?></h1>
                    <br>
                    <?php echo get_the_excerpt( $blogPost1[0]['ID'] ); ?>
                    <br>
                </div>

My Content:

https://youtu.be/BF2Ksrxu_QY
<div></div>
<div>Ok, so I’m supposed to be writing a review for this month’s film review- but I’m genuinely struggling to keep a straight face, let alone to not just crack up laughing. ‘Why?’, you may ask; well, the reason is: this month’s film is <i>The Snowman</i>, and I don’t think I’ve laughed so hard at something so unintentionally funny, in such a long time. Yes, I know what you’re thinking- but isn’t <i>The Snowman</i> that new crime thriller film with what’s-his-name-<i>Fassbender</i> in it? And the answer is yes, it is- but I think the filmmakers have missed a trick here, it really should’ve just been made as an intentional comedy!</div>
<div></div>
<!--more-->
<div>It is safe to say that when I heard Swedish Director, <i>Tomas Alfredson</i> (who has made such greats as: <i>Tinker Tailor Soldier Spy</i> and <i>Let The Right One In</i>), was going to be taking the helm of this project, I was excited to watch the finished product (despite the fact that <i>Martin Scorsese</i> had been the original Director on board); however the Crime novel, penned by Norwegian writer, <i>Jo Nesbø</i> (which also happens to be the 7th book in the ‘Detective Harry Hole’ series), was not in safe hands- as it turns out.</div>

I am not sure what im doing wrong or what I should be doing instead.

1

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;
  • Thanks ive currently got this on a custom page and im trying both of your ways and it doesnt seem to be doing anything it just seems to be empty – Dev Daniel Nov 29 '17 at 13:25
  • Is the title appearing? – Jacob Peattie Nov 29 '17 at 13:31
  • I am getting the title of the page and not the post when using the second option. – Dev Daniel Nov 29 '17 at 14:09

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.