regarding the first question:
reminder:
the ‘read more’ link is added via a filter on excerpt_more
which transforms the iconic three dots … into a link (and those three dots get only added in certain circumstances).
there are a few scenarios:
– there is a hand-written excerpt -> no dots … (therfore also no ‘read more’ link); you might want the ‘read more’ link because the full post is so different from your manual excerpt.
— to add the ‘read more’ in this case would be easy enough to use a filter on 'get_the_excerpt'
based on the conditional has_excerpt()
;
– there is a ‘more tag’ in the cleaned up content, before the excerpt length limit -> no dots … (therfore also no ‘read more’ link); that location might not be what you really intended, and also what is shown is missing formatting and images etc.
— in this case, to add the ‘read more’ link, you could check for the ‘more tag’ in the content, for example using strpos( $post->content, '<!--more-->' )
;
— however, this ‘more tag’ could be within the excerpt length limit, and would therefore shorten the excerpt and leave it without the dots, or it could be anywhere outside the excerpt length limit and would not interfere with the excerpt and the dots … giving you a ‘read more’ link.
—- you will need to check where the ‘more tag’ actually is within the content, and add the ‘read more’ link depending on that position.
– the cleaned up content (after strip tags etc) is shorter than the excerpt length limit; it would not get any three dots … (therfore also no ‘read more’ link); although it might contain images, links, formatting like lists etc which makes it worthwhile to view the full post.
— there is also a conditional check for that.
the code:
//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 .= '… <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' manually done, so Read More', 'textdomain' ) . ' »</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 .= '… <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' cut short by \'more tag\', so Read More', 'textdomain' ) . ' »</a></span>';
// CHECKING FOR EXCERPT BEING SHORT BECAUSE OIF SHORT CONTENT
} elseif( strlen( $text ) == strlen( $excerpt_of_raw ) ) {
$text .= '… <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' too short for excerpt, but Read More anyway', 'textdomain' ) . ' »</a></span>';
}
return $text;
}
checked in functions.php of a child theme in Twenty Twenty and in Iconic One.
I added different texts to the ‘read more’ links to point out the different scenarios, in production, this could be all the same ‘read more’ text …