[Resolved] Excerpts, change to charaters not words

Home Forums Support [Resolved] Excerpts, change to charaters not words

Home Forums Support Excerpts, change to charaters not words

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #836432
    Thomas

    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 ?

    #837081
    Tom
    Lead Developer
    Lead Developer

    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 πŸ™‚

    #837505
    Thomas

    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 ? πŸ™‚

    #838193
    Tom
    Lead Developer
    Lead Developer

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

    Let me know πŸ™‚

    #839583
    Thomas

    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 πŸ™‚ ?

    #840020
    Tom
    Lead Developer
    Lead Developer

    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.

    #840032
    Thomas

    Wow that worked perfectly, awesome thanks A lot πŸ™‚

    #840230
    Tom
    Lead Developer
    Lead Developer

    You’re welcome πŸ™‚

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.