[wp-trac] [WordPress Trac] #28278: Smart Excerpt

WordPress Trac noreply at wordpress.org
Fri May 16 02:39:57 UTC 2014


#28278: Smart Excerpt
-------------------------------+-----------------------------
 Reporter:  GreatBlakes        |      Owner:
     Type:  enhancement        |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  3.9.1
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 I've been using an enhanced excerpt function in all of my themes for the
 past few years, so I thought I'd post it here to see if it's worth
 considering. The problem I was running into was that the_excerpt didn't
 allow for a few parameters that I wanted to customize per iteration. The
 "Continue Reading" text, the excerpt length, and whether the ellipses
 should appear.

 On top of that, I didn't like having to choose between the_excerpt() and
 get_the_excerpt, so I enhanced my function to accept a post ID as the
 first parameter, and if not, it would fallback to the current post ID (so
 it could be used both inside or outside the loop).

 Then, I decided that instead of just using the excerpt from a post, it
 should pull the content if the excerpt was left empty. This would allow my
 to call my function for all instances, and the user could decide if they
 want a specific excerpt to appear for a specific post.

 I'm not sure if this would be better left as a plugin or not, and if not,
 I'm also not sure if I'm advocating an update to the_excerpt or if it
 would be an additional function like smart_excerpt() or something. Anyway,
 here's my code:

 {{{
 //Pulls the excerpt for the current (or designated post ID). If no excerpt
 is present, it pulls from the content instead.
 //Several ways to implement this:
         //Inside the loop: echo nebula_the_excerpt('Read more »',
 20, 1);
         //Outside the loop: echo nebula_the_excerpt(572, 'Read more
 »', 20, 1);
 //First parameter is the post ID (optional). Allows it to be called
 outside the loop. Must be an integer!
 //Second parameter is the "Read More" text (optional).
 //Third parameter is the length (optional).
 //Fourth parameter is the ellipsis (optional). Boolean.

 function nebula_the_excerpt( $postID=false, $more=false, $length=55,
 $hellip=false ) {

         if ( $postID ) {
                 if ( !is_int($postID) ) {
                         if ( $length == 0 || $length == 1 ) {
                                 $hellip = $length;
                         } else {
                                 $hellip = false;
                         }

                         if ( is_int($more) ) {
                                 $length = $more;
                         } else {
                                 $length = 55;
                         }

                         $more = $postID;
                         $postID = false;
                 } else {
                         $the_post = get_post($postID);
                 }
         }

         if ( $the_post ) {
                 if ( $the_post->post_excerpt ) {
                         $string = strip_tags($the_post->post_excerpt,
 '<p>');
                 } else {
                         $string = strip_tags($the_post->post_content,
 '<p>');
                 }
         } else {
                 if ( get_the_excerpt() ) {
                     $string = strip_tags(get_the_excerpt(), '<p>');
                 } else {
                     $string = strip_tags(get_the_content(), '<p>');
                 }
         }

         $string = string_limit_words($string, $length); //Note that this
 function is just a string limiter. I can post the code if needed.

         if ( $hellip ) {
                 if ( $string[1] == 1 ) {
                         $string[0] .= '… ';
                 }
         }

         if ( isset($more) && $more != '' ) {
                 $string[0] .= ' <a class="nebula_the_excerpt" href="' .
 get_permalink($postID) . '">' . $more . '</a>';
         }

         return $string[0];
 }
 }}}

 Currently, It is not the most condensed snippet there is, and I'm sure
 some better programmers could optimize it considerably, but the general
 idea is there.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28278>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list