Get excerpt using get_the_excerpt outside a loop

I have a code that call get_the_title() and it works, but get_the_excerpt() return empty. How can i make it work?

This code is inside a plugin called “WP Facebook Open Graph protocol”. Here’s the part i want to change:

if (is_singular('post')) {
  if (has_excerpt($post->ID)) {
    echo "t<meta property='og:description' content='".esc_attr(strip_tags(get_the_excerpt($post->ID)))."' />n";
  }else{
    echo "t<meta property='og:description' content='". [?] ."' />n";
  }
}else{
  echo "t<meta property='og:description' content='".get_bloginfo('description')."' />n";
}

Here, has_excerpt always fail, and get_the_excerpt($post->ID) don’t work anymore (deprecated).

So, how can i display the excerpt there?

ps: I’m using “Advanced Excerpt” plugin as well

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

I found this question when looking how to do this without the post object.

My additional research turned up this slick technique:

$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));

Method 2

Since it seems you already have the post object you need the excerpt for, you can just force things to work:

setup_postdata( $post );
$excerpt = get_the_excerpt();

The setup_postdata() function will globalize the $post object and make it available for regular old loop function. When you’re inside the loop, you call the_post() and it sets things up for you … outside of the loop you need to force it manually.

Method 3

Try this:

Create a new function in functions.php and then call it from wherever.

function get_excerpt_by_id($post_id){
    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
    $excerpt_length = 35; //Sets excerpt length by word count
    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
    $words = explode(' ', $the_excerpt, $excerpt_length + 1);

    if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '…');
        $the_excerpt = implode(' ', $words);
    endif;

    $the_excerpt = '<p>' . $the_excerpt . '</p>';

    return $the_excerpt;
}

Here’s a post describing the code.

Method 4

Now you can simply use the get_the_excerpt( $postID ) function.
Since: WordPress 4.5.0 introduced the $post parameter.

Method 5

got it using my_excerpt($post->post_content, get_the_excerpt()) and using the my_excerpt() function from Using wp_trim_excerpt to get the_excerpt() outside the loop

Method 6

In case you don’t have the post object, here’s a short function like the one from Withers.

function get_excerpt_by_id($post_id){
    $the_post = get_post($post_id);
    $the_excerpt = $the_post->post_excerpt; 
    return $the_excerpt;
}

Method 7

This is for when you want to use get_the_excerpt() outside the loop:

function custom_get_excerpt($post_id) {
    $temp = $post;
    $post = get_post($post_id);
    setup_postdata($post);

    $excerpt = get_the_excerpt();

    wp_reset_postdata();
    $post = $temp;

    return $excerpt;
}

Method 8

If you’d like to generate the excerpt automatically from the content in one line – you can use wp_trim_words function like this:

// 30 is the number of words ehere
$excerpt = wp_trim_words(get_post_field('post_content', $post_id), 30);

Method 9

$trimexcerpt = get_the_content();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 18, $more = '… ' ); 
echo $shortexcerpt;


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x