Site logo

Archived topic

How to display 'read more' when using the excerpt field

31 replies · Started by Dave Foy on March 21, 2017

Viewing posts 1–15 of 32

On a site I'm working on, the client has been using the excerpt field in the blog post edit screen to control the excerpt that's displayed on archive pages.

In 'Customizer > Blog > Blog Content' I have Blog Post Content set to 'Show Excerpt', and the 'Read More' label set as 'Read more'.

I've noticed that with this set-up, no 'Read more' link appears. It only appears if you set 'Show Excerpt' and have used the 'More' tag in the post, rather than the excerpt field.

Is there a way to display the 'Read more' link in both cases?

Thanks. :)

Interesting, let me look into this and get back to you. Sounds like a bug.

No worries Tom. I'm pretty sure I had it on my main site too, I had to go with 'more' tag because I needed the 'read more' link.

Let me know if you need access to anything at my end.

Looking into this more, it looks like WordPress doesn't add the necessary filters to the manual excerpt custom field.

I'll research some more to see if I can come up with a solution for you :)

Had to dig into WP core to figure this one out.

It seems WP skips all of the excerpt_length and excerpt_more filters when the custom excerpt is used.

So, we have to hook our read more link into a filter that it is using:

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
	$output = $excerpt;
	
	if ( has_excerpt() ) {
		$output = sprintf( '%1$s <a href="%2$s">Read more</a>',
			$excerpt,
			get_permalink()
		);
	}
	
	return $output;
}

Brilliant. Works a treat!

Huge thanks Tom. That solves an issue for me on several sites now.

Glad I could help :)

Added the above filter to my child theme functions.php file to no avail.

And when we do get the "Read More" to appear when adding manual excerpts, will the CSS edit you've supplied earlier apply to this?

.read-more,
.read-more:visited {
background: none repeat scroll 0 0 #222;
color: #fff;
display: table;
margin-top: 1.5em;
padding: 10px 20px;
}

.read-more:hover {
background:#666;
color:#FFF;
}

The above function will only work when the excerpt field is in use.

That CSS should work.

I was looking to add read more to posts with excerpt, searched the support forum, found the answer... Thanks Tom :-)

Awesome! :)

Hi Tom - this snippet is what I've been looking for (working around) for a while - thank you.

Is there any way to get the link text to reflect what has been set as the 'Read more label' in the Customiser (rather than having to change the snippet code when the Customiser is edited)?

Cheers,

Andy

PS. (off-topic) 1.4 header image handling is so much better now - I've had to undo a whole bunch of workaround code, but is totally worth it.

Absolutely! Try this:

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
	$output = $excerpt;
        $settings = wp_parse_args( 
		get_option( 'generate_blog_settings', array() ), 
		generate_blog_get_defaults() 
	);
	
	if ( has_excerpt() ) {
		$output = sprintf( '%1$s <a href="%2$s">%3$s</a>',
			$excerpt,
			get_permalink(),
                        wp_kses_post( $settings['read_more'] )
		);
	}
	
	return $output;
}

Great to hear you're enjoying 1.4! Definitely a big change :)

Awesome as always - thank you, Tom!

You're welcome :)

This archived topic is closed to new replies.