Community Forums › Forums › Archived Forums › General Discussion › Lifestyle Pro: Excerpt Limit in Featured Post and Page Widget not limiting
Tagged: Post Excerpt Limits
- This topic has 8 replies, 2 voices, and was last updated 9 years, 11 months ago by Tony @ AlphaBlossom.
-
AuthorPosts
-
May 16, 2014 at 12:01 pm #105516snohoboMember
Hey,
Wanting to use excerpts on my home page using the Featured Post and Feature Post Widget but the character limit doesn't seem to work. Is this feature only enabled when I select Content Limit from the drop down?
Is there a bit of code I can add to the functions.php to enable this? Iwould prefer to not have to manually count characters in the excerpt filed of my posts.
http://www.thelovedrive.comMay 16, 2014 at 12:17 pm #105519snohoboMemberAllow me to be a bit more specific. I would like to limit the excerpt to a certain word length and to add [...] at the end of that word with a link to [more...] to link to the specific post.
I was able to find this code which adds the [more] link but haven't found a workaround for limiting the amount of text in the manual excerpt.
// Changing excerpt more - only works where excerpt IS hand-crafted
add_filter('get_the_excerpt', 'manual_excerpt_more');
function manual_excerpt_more($excerpt) {
$excerpt_more = '';
if( has_excerpt() ) {I also understand that I could simply manually limit my excerpts to a certain character length but this would save me lots of time as I don't know another way to have exactly (or close enough to) 100 characters to display without manually counting them...
May 16, 2014 at 12:58 pm #105526Tony @ AlphaBlossomMemberHello,
This should work for what you want http://www.transformationpowertools.com/wordpress/automatically-shorten-manual-excerpt
I modified it to add the [more] link:
function wp_trim_all_excerpt($text) { // Creates an excerpt if needed; and shortens the manual excerpt as well global $post; $raw_excerpt = $text; if ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); } $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 10); $excerpt_more = apply_filters('excerpt_more', ' ' . '<a class="more-link" href="' . get_permalink() . '" rel="nofollow">' . __('[more]', 'enterprise') . '</a>'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); //since wp3.3 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3 } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'wp_trim_all_excerpt');
If you have a manual excerpt it will shorten that. If not, it will use the content instead for the excerpt and shorten that. I'd recommend changing the "wp" in the function name to your own unique prefix (less chance of conflicting with another plugin, etc).
Hope that helps!
Tony
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
May 16, 2014 at 1:06 pm #105530Tony @ AlphaBlossomMemberHi,
This should work: http://www.transformationpowertools.com/wordpress/automatically-shorten-manual-excerpt
I modified it to add the [more] link:
function wp_trim_all_excerpt($text) { // Creates an excerpt if needed; and shortens the manual excerpt as well global $post; $raw_excerpt = $text; if ( '' == $text ) { $text = get_the_content(''); $text = strip_shortcodes( $text ); $text = apply_filters('the_content', $text); $text = str_replace(']]>', ']]>', $text); } $text = strip_tags($text); $excerpt_length = apply_filters('excerpt_length', 10); $excerpt_more = apply_filters('excerpt_more', ' ' . '<a class="more-link" href="' . get_permalink() . '" rel="nofollow">' . __('[more]', 'enterprise') . '</a>'); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); //since wp3.3 return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3 } remove_filter('get_the_excerpt', 'wp_trim_excerpt'); add_filter('get_the_excerpt', 'wp_trim_all_excerpt');
If you have a manual excerpt it will shorten that. If not, it will use the content instead for the excerpt and shorten that.
Hope that helps!
Tony
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
May 16, 2014 at 1:18 pm #105534Tony @ AlphaBlossomMemberWell, I'm not sure why I can't post my solution here (saves but won't show on page), so I posted my solution here to your problem:
http://www.alphablossom.com/code-snippets/shorten-length-wordpress-manual-excerpt-link-post/
Hope that helps,
Tony
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
May 16, 2014 at 1:30 pm #105539snohoboMemberThis is great. Just what I needed and thank you so much for your speediness.
The only thing that would make this 100% is if the code could add [...] at the end of the last word before displaying the [more] link.
That would top it off but I'm very happy with the solution, super clean.
Ps. Not sure why you weren't able to post it either...I did receive a 'test' message from you but you must have removed it by the time I logged in.
May 16, 2014 at 1:36 pm #105542Tony @ AlphaBlossomMemberYea, it's the first time it's happened...after hitting submit it would take me to this page but my post wouldn't show. Then when I tried to "resubmit" it, I got an error saying 'looks like you already said that.' Never happened before, but glad you could get it from my site.
You can change this line:
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '...<a class="more-link" href="' . get_permalink() . '" rel="nofollow">[more]</a>' );
Just added "..." before the link, I think that's what you want?
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
May 16, 2014 at 1:39 pm #105543snohoboMemberTotally nailed it, thanks so much!
May 16, 2014 at 1:42 pm #105544Tony @ AlphaBlossomMemberAwesome, glad I could help. Have a great weekend!
Tony Eppright | http://www.AlphaBlossom.com | Follow me on twitter @_alphablossom
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.