- This topic has 31 replies, 12 voices, and was last updated 5 years, 5 months ago by Leo.
-
AuthorPosts
-
March 21, 2017 at 2:51 am #295109Dave Foy
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. π
March 21, 2017 at 9:10 am #295276TomLead DeveloperLead DeveloperInteresting, let me look into this and get back to you. Sounds like a bug.
March 22, 2017 at 1:40 am #295578Dave FoyNo 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.
March 23, 2017 at 11:05 am #296413TomLead DeveloperLead DeveloperLooking 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 π
March 26, 2017 at 10:47 am #297608TomLead DeveloperLead DeveloperHad 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; }
March 27, 2017 at 4:50 am #297884Dave FoyBrilliant. Works a treat!
Huge thanks Tom. That solves an issue for me on several sites now.
March 27, 2017 at 9:37 am #298002TomLead DeveloperLead DeveloperGlad I could help π
April 3, 2017 at 5:16 pm #301297CarolAdded 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;
}April 3, 2017 at 7:05 pm #301319TomLead DeveloperLead DeveloperThe above function will only work when the excerpt field is in use.
That CSS should work.
June 2, 2017 at 7:28 am #327665HuguesI was looking to add read more to posts with excerpt, searched the support forum, found the answer… Thanks Tom π
June 2, 2017 at 9:13 am #327724TomLead DeveloperLead DeveloperAwesome! π
August 21, 2017 at 3:55 am #370310AndyHi 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.
August 21, 2017 at 9:42 am #370520TomLead DeveloperLead DeveloperAbsolutely! 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 π
August 21, 2017 at 2:17 pm #370744AndyAwesome as always – thank you, Tom!
August 21, 2017 at 2:34 pm #370750TomLead DeveloperLead DeveloperYou’re welcome π
-
AuthorPosts
- The topic ‘How to display 'read more' when using the excerpt field’ is closed to new replies.