[Resolved] Custom ‘Read More’ Text for specific Category

Home Forums Support [Resolved] Custom ‘Read More’ Text for specific Category

Home Forums Support Custom ‘Read More’ Text for specific Category

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1338573
    Vish

    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

    #1338608
    David
    Staff
    Customer Support

    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 );
    #1338767
    Vish

    Thanks a ton, David. 🙂

    #1338916
    David
    Staff
    Customer Support

    You’re welcome

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.