Support » Plugins and Hacks » Hacks » Get […] after custom excerpt

  • Resolved rebah

    (@rebah)


    I added this code

    <?php echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' ) ); ?>

    for a custom excerpt in a slider header. That worked, but how do I get the […] behind that. I know it has to do with the excerpt_more, but I dont know how to integrate it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Take a look at wp_trim_excerpt()

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

    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.

    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.

    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 […]

    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), ' ' ) . '[...]' ); ?>

    I used the code in your example but the ellipsis aren’t showing…

    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

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Get […] after custom excerpt’ is closed to new replies.