Site logo

Archived topic

Excerpts, change to charaters not words

7 replies · Started by Thomas on March 12, 2019

Viewing posts 1–8 of 8

Hi

I'm having a problem on my site where im shoing blog posts on the frontpage. The excerpt are using a word count of 55, that fits the danish version perfectly. But on our Greenlandic site, that looks awfull. I tried changing it to characters

Then i tried adding this one

function custom_short_excerpt($excerpt){
return substr($excerpt, 0, 200);
}
add_filter('the_excerpt', 'custom_short_excerpt');

It sort of works, but i lose the read more buttons - are there any other ways of doing it and still get the read more buttons ?

Hi there,

You could try something like this:

add_filter( 'the_excerpt', function( $excerpt ) {
    $excerpt = substr( $excerpt, 0, 200 );

    $button = sprintf( '<p class="read-more-container"><a title="%1$s" class="read-more content-read-more button" href="%2$s">%3$s%4$s</a></p>',
        the_title_attribute( 'echo=0' ),
        esc_url( get_permalink( get_the_ID() ) . apply_filters( 'generate_more_jump','#more-' . get_the_ID() ) ),
        __( 'Read more', 'gp-premium' ),
        '<span class="screen-reader-text">' . get_the_title() . '</span>'
    );

    return $excerpt . $button;
} );

Let me know :)

Hi Tom,

Thats awesome thanks Alot :) it sort of works, i get the read more links now, but i would prefer if i could have the blue buttons like it is as standard, also the standard buttons are wpml compatible so that it gets the translated text, is that possibel somehow ? :)

I just edited the code above - can you give it another shot?

Let me know :)

That worked perfectly, now i have the buttons :) Do you know if there are there any solutions out there so i can use 2 different code snippets based on the current language so i can translate the button text :) ?

You could try this:

add_filter( 'the_excerpt', function( $excerpt ) {
    $excerpt = substr( $excerpt, 0, 200 );

    $text = 'Read more';

    if ( 'de_DE' === get_locale() ) {
        $text = 'Read more text for de_DE';
    }

    $button = sprintf( '<p class="read-more-container"><a title="%1$s" class="read-more content-read-more button" href="%2$s">%3$s%4$s</a></p>',
        the_title_attribute( 'echo=0' ),
        esc_url( get_permalink( get_the_ID() ) . apply_filters( 'generate_more_jump','#more-' . get_the_ID() ) ),
        $text,
        '<span class="screen-reader-text">' . get_the_title() . '</span>'
    );

    return $excerpt . $button;
} );

You just need to update de_DE to your language, and change the text.

Wow that worked perfectly, awesome thanks A lot :)

You're welcome :)

This archived topic is closed to new replies.