Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

A Better WordPress Excerpt by ID Function

Posted on December 5, 2011 by Pippin in Functions, Quick Tips, Tutorials 14 Comments
Home» Functions » A Better WordPress Excerpt by ID Function
Tweet
Love It - 0

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!

Tweet Follow @pippinsplugins
excerpt, post

14 comments on “A Better WordPress Excerpt by ID Function”

  1. eBar Solutions says:
    January 9, 2012 at 4:24 pm

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

    Reply
    • Pippin says:
      January 10, 2012 at 10:32 am

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

  2. Neha says:
    May 26, 2012 at 8:02 am

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

    Reply
    • Pippin says:
      May 26, 2012 at 7:59 pm

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

  3. Dustin says:
    June 4, 2012 at 2:44 am

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

    Reply
  4. ricardorenderos says:
    August 9, 2012 at 9:13 pm

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

    Reply
    • ricardorenderos says:
      August 9, 2012 at 9:16 pm

      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 says:
      August 10, 2012 at 9:20 am

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

  5. Rich Staats says:
    August 20, 2012 at 3:04 pm

    I can’t seem to pass my variable to this. I’m using the SMOF Options Framework”> to hand pick posts for the home page, and would love to include an excerpt. I’ve included this in functions.php and here is my Gist: https://gist.github.com/3407327

    If I pass a post id directly into pippin_excerpt_by_id It works fine…

    Thanks in advance.

    Reply
    • Pippin says:
      August 20, 2012 at 7:48 pm

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

  6. Rich Staats says:
    August 20, 2012 at 8:27 pm

    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.

    Reply
    • Pippin says:
      August 21, 2012 at 11:23 am

      Try passing it like this:

      (int)$marketI
    • Rich Staats says:
      August 21, 2012 at 7:16 pm

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

    • Pippin says:
      August 22, 2012 at 11:30 am

      Glad to help :)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

Sorry, no related items found.

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes
  • Plugin Development 101 – Registering a Custom Post Type
  • Plugin Development 101 – Intro to Actions

Latest Tutorials

  • Test Your Plugins with RTL (0)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

  • Plugin Development 101 – Introduction to Adding Dashboard Menus (1)

    Adding new menus, both top level and sub level, to the WordPress Dashboard is a really common task for plugins...

Enter your email to receive automated updates when new posts are published

Latest Tweets

  • @om4james That&#039;s what I was looking for, thanks. That was not easy to find @woothemes
    May 24, 2013
  • @om4james Whoops, sorry, I meant the WooCommerce ones @woothemes
    May 24, 2013
  • @woothemes What&#039;s up with not having basic documentation on short codes on the docs page? Am I just blind?
    May 24, 2013

Topics

Sugar Event Calendar add_options_page Tom McFarlin contextual help attachments campaign monitor register_setting wp_enqueue_script hook Rémi Corson featured shortcodes the_content plugin authors do_action attachment image forms short codes login Related posts mail chimp bbpress comments recent posts apply_filters post types short code taxonomies custom post type images gallery Ajax Stripe taxonomy jquery users widgets add_filter add_action easy content types widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins