WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get [...] after custom excerpt (9 posts)

  1. rebah
    Member
    Posted 2 years ago #

    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.

  2. Joey Kudish
    Member
    Posted 2 years ago #

    Take a look at wp_trim_excerpt()

  3. Jamàl
    Member
    Posted 2 years ago #

    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;
    }
  4. rebah
    Member
    Posted 2 years ago #

    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.

  5. Jamàl
    Member
    Posted 2 years ago #

    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.

  6. rebah
    Member
    Posted 2 years ago #

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

  7. Jamàl
    Member
    Posted 2 years ago #

    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), ' ' ) . '[...]' ); ?>
  8. rebah
    Member
    Posted 2 years ago #

    I used the code in your example but the ellipsis aren't showing...

  9. rebah
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic