Memorial Day Sale! Get 15% off GP Premium with the discount code: MEMORIALDAY19

[Resolved] Excerpts, change to charaters not words

Home Forums Support Excerpts, change to charaters not words

This topic contains 7 replies, has 2 voices, and was last updated by  Tom 2 months, 1 week ago.

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 ?

    GeneratePress 2.2.2
    GP Premium 1.7.8
    #837081

    Tom 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

    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

    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
Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.