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 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

share|improve this question
    
ok, got it using my_excerpt($post->post_content, get_the_excerpt()) and using the my_excerpt() function from wordpress.stackexchange.com/questions/6961/… –  ariel Aug 24 '11 at 0:44
2  
Please add solution you came up with as an answer, so this doesn't haunt site as unanswered question. :) –  Rarst Sep 11 '11 at 21:56
add comment

4 Answers

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment
up vote 1 down vote accepted

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

share|improve this answer
add comment

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;
}
share|improve this answer
    
But the asker has a post object as stated in the question. –  toscho Nov 25 '12 at 19:51
add comment

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.