Quantcast
Viewing all articles
Browse latest Browse all 4

‘Read more’ links for all scenarios of excerpts

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.wordpress.org/reference/hooks/excerpt_more/;
– WordPress allows to change the set lentgth limit with a filter function – https://developer.wordpress.org/reference/hooks/excerpt_length/;
– and most interesting, it also allows to customize the whole excerpt with yet another filter function – https://developer.wordpress.org/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-->' ) &amp;&amp; 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 OIF 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;
}

Viewing all articles
Browse latest Browse all 4

Trending Articles