Take a look at wp_trim_excerpt()
J
(@jamalorg)
Try the function below, which hooks to get_the_excerpt filter.
/* Hook the rebah_custom_excerpt_more() function to 'get_the_excerpt' filter hook */
add_filter( 'get_the_excerpt', 'rebah_custom_excerpt_more' );
/**
* Add [...] after custom excerpts.
*
* @param string $output [...]
* @return string $output [...]
*/
function rebah_custom_excerpt_more( $output ) {
if ( has_excerpt() && ! is_attachment() ) {
$output .= ' […]';
}
return $output;
}
Thread Starter
rebah
(@rebah)
I looked at the wp_trim_excerpt already, but I still dont know what to do. I’m a total newbie to code and WP.
Jamàl, I added this code to functions.php. Is that the right file?
Do I also have to change the code I used mentioned in my first post or should it just work. So far it doesn’t.
J
(@jamalorg)
Rebah,
I added this code to functions.php. Is that the right file?
Yes. The code goes into your active theme’s functions.php file.
Do I also have to change the code I used mentioned in my first post[…]
Yes. Remove that code, and use the standard <?php the_excerpt(); ?> template tag.
The function I posted in my previous post checks if the post have a custom excerpt, and the post in question is not an attachment; it then appends [...] at the end of the custom excerpt text if the condition is true.
Thread Starter
rebah
(@rebah)
I think that I maybe wasn’t clear enough about the custom excerpt, because now with your method I can’t define the characters of the excerpt anywhere, right?
My intention is this:
On my homepage I have the excerpt of my posts, and beside that I have a slider as header with the same posts. I want the excerpt in the slider to be shorter then the excerpt on the page below. That worked with the code in my first post, but without the […]
J
(@jamalorg)
Well, you can append the ellipsis at the end of your original code.
Example:
<?php echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' ) . '[...]' ); ?>
Thread Starter
rebah
(@rebah)
I used the code in your example but the ellipsis aren’t showing…
Thread Starter
rebah
(@rebah)
So after some experimenting i got it today. I used:
get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' )); ?> [...]
Thanks for the help