One of the things I’d always disliked about the core get_the_excerpt() function in WordPress is that it does not allow you to retrieve an excerpt based on a post ID or object. This limitation means that this function is essentially useless in custom WordPress loops that do not have the global $post object set up. So I’ve written a little function that will allow you to pass the ID or object of your post and it will return the excerpt. The function also accepts a couple of other parameters that allow you to specify the length of the excerpt, the HTML tags tat should be allowed, and any extra content to append to the end of the excerpt, such as an ellipsis.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
* Gets the excerpt of a specific post ID or object
* @param - $post - object/int - the ID or object of the post to get the excerpt of
* @param - $length - int - the length of the excerpt in words
* @param - $tags - string - the allowed HTML tags. These will not be stripped out
* @param - $extra - string - text to append to the end of the excerpt
*/
function pippin_excerpt_by_id($post, $length = 10, $tags = '<a><em><strong>', $extra = ' . . .') {
 
	if(is_int($post)) {
		// get the post object of the passed ID
		$post = get_post($post);
	} elseif(!is_object($post)) {
		return false;
	}
 
	if(has_excerpt($post->ID)) {
		$the_excerpt = $post->post_excerpt;
		return apply_filters('the_content', $the_excerpt);
	} else {
		$the_excerpt = $post->post_content;
	}
 
	$the_excerpt = strip_shortcodes(strip_tags($the_excerpt), $tags);
	$the_excerpt = preg_split('/\b/', $the_excerpt, $length * 2+1);
	$excerpt_waste = array_pop($the_excerpt);
	$the_excerpt = implode($the_excerpt);
	$the_excerpt .= $extra;
 
	return apply_filters('the_content', $the_excerpt);
}

You can use the function to display the excerpt like this:

echo pippin_excerpt_by_id($your_post_object);

Or like this (with parameters customized:

echo pippin_excerpt_by_id($your_post_object, 20, '<a><em>', '');

The second one will show an excerpt of 20 words, will only allow A an EM tags, and will not show an ellipsis.

Enjoy!

  1. eBar Solutions

    How can this be added to your posts by taxonomy widget pro?

    • Pippin

      Just replace the functions that are currently used in the plugin with this one.

  2. Neha

    If u elaborate slightly , that will be better to understand.

    • Pippin

      Tell me what you’re having trouble with and I will be more than happy to elaborate on it.

  3. Dustin

    Thanks Pippin! This was very helpful and one of the only places that have the solution to this problem.

  4. ricardorenderos

    Could you please show us how to execute this with live code? For example… Here’s some code I’ve got:

    post_content;
    $content = apply_filters(‘the_content’, $content);
    $content = str_replace(‘]]>’, ‘]]>’, $content);
    echo $content;
    ?>

    • ricardorenderos

      I’m sorry, i missed to wrap my code. Here goes round two:

      post_content;
      $content = apply_filters('the_content', $content);
      $content = str_replace(']]>', ']]>', $content);
      echo $content;
      ?>

    • Pippin

      I think your code is still incomplete, can you paste it into snippi.com or wrap it in PRE tags?

    • Pippin

      If it’s not working with $marketID then that means that that variable isn’t setup correctly.

  5. Rich Staats

    Oh, I’m sure it’s an error on my end, but $marketID is working when passed through the other functions like get_the_title($marketID); so I was hoping you’re second set of eyes would see something obvious that I was overlooking.

    It works as expected if i just set up $marketID like: $marketID = 184;, so it must be how SMOF is bringing in the ID: $marketID = $data['ssm_market_post'];.

    But if I echo $data['ssm_market_post']; it outputs 184, so I am scratching my head where I am goofing up.

    • Pippin

      Try passing it like this:

      (int)$marketI
    • Rich Staats

      Yup, that did it. Thanks a bunch. You just found a new customer.

    • Pippin

      Glad to help 🙂

  6. Frank

    Very nice function thanks for sharing, especially in combination with Advanced Custom Fields Plugin

  7. Frank

    should

    $the_excerpt = strip_shortcodes(strip_tags($the_excerpt), $tags);

    not be

    $the_excerpt = strip_shortcodes(strip_tags($the_excerpt, $tags));

  8. Eric McNiece

    Do you think that WordPress takes a performance hit for loading the entire $post object for a single image? I wonder if it would be more efficient to perform a single-cell database query…

    • Pippin

      No because WordPress already loads the $post object, and it is usually pulled from cache anyhow.

  9. Daniel

    Couple notes – without Frank’s edit, the function will still strip all HTML tags (they are being passed as a parameter to ‘strip_shortcodes’ rather than ‘strip_tags’).

    That’s probably good because of #2 though: you’re likely to leave tags unclosed if you do allow them through. Depending on your HTML structure, this could wreak havoc (although in many cases the browser will probably handle it okay).

Comments are closed.