Opened 12 years ago
Closed 7 years ago
#28278 closed enhancement (wontfix)
Smart Excerpt
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 3.9.1 |
| Component: | Posts, Post Types | Keywords: | needs-patch dev-feedback close |
| Focuses: | Cc: |
Description
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.
Change History (5)
#2
@
12 years ago
I personally believe it would be best left as a plugin as not all WordPress themes make use of the excerpt field. Maybe wait for other, more seasoned, core developers to chime in.
#4
@
9 years ago
- Keywords close added
It is very unlikely that get_the_excerpt and the_excerpt will be replaced by another function.
I believe the whole custom function could be replaced with filters and conditionals. If want a different excerpt length on a certain page you can use a conditional. With the following code example I can change the excerpt length just on the category archive.
<?php prefix_custom_excerpt_length( $length ) { if( is_category() ) { $length = 25; } return $length; } add_filter( 'excerpt_length', 'prefix_custom_excerpt_length' );
#5
@
7 years ago
- Milestone Awaiting Review deleted
- Resolution set to wontfix
- Status changed from new to closed
Hi @GreatBlakes and thank you for your submission. As @grapplerulrich noted above it is very unlikely that the functions themselves will be replaced.
Given the age of this ticket and that no patch has been added I will close it as wontfix.
Thanks again for your suggestions!
I've recently made some pretty heavy adjustments to enhance this function:
//Several ways to implement this: //Inside the loop (or outside the loop for current post/page): nebula_the_excerpt('Read more »', 20, 1); //Outside the loop: nebula_the_excerpt(572, 'Read more »', 20, 1); function nebula_the_excerpt( $postID=0, $more=0, $length=55, $hellip=0 ) { if ( $postID && is_int($postID) ) { $the_post = get_post($postID); } else { if ( $postID != 0 || is_string($postID) ) { if ( $length == 0 || $length == 1 ) { $hellip = $length; } else { $hellip = false; } if ( is_int($more) ) { $length = $more; } else { $length = 55; } $more = $postID; } $postID = get_the_ID(); $the_post = get_post($postID); } if ( $the_post->post_excerpt ) { $string = strip_tags(strip_shortcodes($the_post->post_excerpt), ''); } else { $string = strip_tags(strip_shortcodes($the_post->post_content), ''); } $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]; }