• Resolved zazzou

    (@zazzou)


    Hi,
    I have two posts displaying on my home page with this code :
    $recentPosts->query('showposts=2');

    When I am on my home page, the excerpt is displaying and I have a link “read more” below.
    But when I have just few words on my article, the excerpt is on the home page but not the link “read more”. Why ?
    I have this on my functions.php :

    function wpdocs_excerpt_more( $more ) {
        if ( ! is_single() ) {
            $more = sprintf( '<p class="read-more"><a class="read-more" href="%1$s">%2$s</a></p>',
                get_permalink( get_the_ID() ),
                __( ' Lire la suite', 'textdomain' )
            );
        }
        return $more;
        }
        add_filter( 'excerpt_more', 'wpdocs_excerpt_more' );

    Thanks for help

Viewing 3 replies - 1 through 3 (of 3 total)
  • That is because there is nothing to read more .. the excerpt is showing all the content you have

    when you exceed the minimum content length, read more is shown

    add some more content or try using more tag in wordpress in wp editor

    You can try adding the read more link manually using get permalink function

    follow up on this comment:

    the excerpt is showing all the content you have

    not quite, because the_excerpt is stripping all html tags, such as images, links, formatting etc from the content, so even if all words are shown, it might be interesting to see the full post.

    I recently checked all possible scenarios for using the_excerpt; and you can add a ‘read-more’ for all of these scenarios, using code.

    here is the text and code from my article:

    When using the function ‘the_excerpt()’ in your code, WordPress by default removes all html tags, shortens the content if applicable and in this case appends those three dots … all depending on the set length limit.

    There are some scenarios where no dots … get added:
    – there is a hand-written excerpt;
    – the cleared up content is shorter than the set lenght limit;
    – there is a ‘more tag’ within the content before the set length limit.

    You can customize the excerpt in some ways:
    – WordPress allows to use a filter function to turn those three dots … into anything , such as a ‘read more’ link – https://developer.ww.wp.xz.cn/reference/hooks/excerpt_more/;
    – WordPress allows to change the set lentgth limit with a filter function – https://developer.ww.wp.xz.cn/reference/hooks/excerpt_length/;
    – and most interesting, it also allows to customize the whole excerpt with yet another filter function – https://developer.ww.wp.xz.cn/reference/hooks/get_the_excerpt/.

    If no dots … are added to the end of the excerpt, the first of those filter hooks does not work, so there is no direct way to add a ‘read more’ link in those scenarios.

    Luckily, there is the filter hook ‘get_the_excerpt’ which allows to append anything – we just have to distinguish between all possible scenarios to avoid adding multiple ‘read more’ links to the excerpt…

    The code:

    /*
    * adding the 'read more' to all scenarios of excerpts when using 'the_excerpt()' function
    */
     
    //TURNING THE TRHEE DOTS ... INTO READ MORE LINK
    add_filter('excerpt_more', 'excerpt_read_more_link');
     
    function excerpt_read_more_link( $more ) {
     
        global $post;
        return '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( 'Read More', 'textdomain' ) . ' &raquo;</a></span>';
     
    }
     
    //ADDING THE READ MORE LINK TO ALL OTHER EXCERPTS
    add_filter( 'get_the_excerpt', 'excerpt_read_more_all_cases' );
     
    function excerpt_read_more_all_cases( $text ) {
     
        global $post;
        $raw = $post->post_content;
        $excerpt_length = apply_filters( 'excerpt_length', 55 );
        $raw_to_more = substr( $raw, 0, strpos( $raw, '<!--more-->' ) );
        $excerpt_of_raw = wp_trim_words( $raw, $excerpt_length, ''); 
        $excerpt_of_raw_to_more = wp_trim_words( $raw_to_more, $excerpt_length, ''); 
     
    // CHECKING FOR MANUALLY WRITTEN EXCERPT
        if( has_excerpt() ) { 
         
            $text .= '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' manually done, so Read More', 'textdomain' ) . ' &raquo;</a></span>';
             
    // CHECKING FOR EXCERPT BEING SHORT BY THE 'MORE TAG'
        } elseif( strpos( $raw, '<!--more-->' ) && strlen( $excerpt_of_raw_to_more ) < strlen( $excerpt_of_raw ) ) {
         
            $text .= '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' cut short by \'more tag\', so Read More', 'textdomain' ) . ' &raquo;</a></span>';
             
    // CHECKING FOR EXCERPT BEING SHORT BECAUSE OF SHORT CONTENT
        } elseif( strlen( $text ) == strlen( $excerpt_of_raw ) ) {
         
            $text .= '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' too short for excerpt, but Read More anyway', 'textdomain' ) . ' &raquo;</a></span>';
             
        }
         
        return $text;
    }
    Thread Starter zazzou

    (@zazzou)

    Thanks for answer !

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Excerpt and “read more”’ is closed to new replies.