Site logo

Archived topic

Custom 'Read More' Text for specific Category

3 replies · Started by Vish on June 23, 2020

Viewing posts 1–4 of 4

Hi

I was wondering how to add a custom 'Read More' text (as seen in blog/archive pages) for a specific category. I want to change the text from 'Read More' (for other categories) to something like 'Learn' for one specific category.

I tried the below code suggested by Leo here:

add_filter( 'option_generate_blog_settings', 'lh_custom_read_more_text' );
function lh_custom_read_more_text( $options ) {
    if ( is_category( '9' ) ) {
	$options['read_more'] = 'My custom read more label'
    }
  
    return $options;
}

I doesn't work. I also tried to make my own with the help of documentation (this and this). But I am not able to get it right.

Any help would be much appreciated.

Thanks

Hi there,

that code will change the read more label when displayed in that category archive.
Instead you would have to use this filter:

https://docs.generatepress.com/article/generate_excerpt_more_output/

With the in_category() function as the conditional tag.
https://developer.wordpress.org/reference/functions/in_category/

Try this PHP Snippet instead:

add_filter( 'generate_excerpt_more_output', function( $more ) {
    if ( in_category('slug') ) {
        return sprintf( ' ... <a class="read-more" title="%1$s" href="%2$s">%3$s</a>',
            the_title_attribute( 'echo=0' ),
            esc_url( get_permalink( get_the_ID() ) ),
            __( 'Custom Text', 'generatepress' )
        );
	}
    return $more;
}, 50 );

Thanks a ton, David. :)

You're welcome

This archived topic is closed to new replies.