🎉 Happy 4th of July! Get 15% off GP Premium with the discount code: 4THOFJULY2020

[Resolved] How to display 'read more' when using the excerpt field

Home Forums Support How to display 'read more' when using the excerpt field

Viewing 15 posts - 1 through 15 (of 32 total)
  • Author
    Posts
  • #295109
    Dave 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. 🙂

    GeneratePress 1.3.46
    GP Premium 1.2.96
    #295276
    Tom
    Lead Developer
    Lead Developer

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

    #295578
    Dave Foy

    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.

    #296413
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #297608
    Tom
    Lead Developer
    Lead Developer

    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;
    }
    #297884
    Dave Foy

    Brilliant. Works a treat!

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

    #298002
    Tom
    Lead Developer
    Lead Developer
    #301297
    Mike

    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;
    }

    #301319
    Tom
    Lead Developer
    Lead Developer

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

    That CSS should work.

    #327665
    Hugues

    I was looking to add read more to posts with excerpt, searched the support forum, found the answer… Thanks Tom 🙂

    #327724
    Tom
    Lead Developer
    Lead Developer
    #370310
    Andy

    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.

    #370520
    Tom
    Lead Developer
    Lead Developer

    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 🙂

    #370744
    Andy

    Awesome as always – thank you, Tom!

    #370750
    Tom
    Lead Developer
    Lead Developer
Viewing 15 posts - 1 through 15 (of 32 total)
  • The topic ‘How to display 'read more' when using the excerpt field’ is closed to new replies.