Support » Theme: GeneratePress » Change Read more text in one category

  • Resolved FeliceAntonio

    (@antonio_09)


    Hi there,
    I wonder why this code found here does not work into my functions.php child theme.
    I tried to change in:

    add_filter( 'generate_excerpt_more_output', function( $more ) {
        if ( in_category( 'NUMBER_CATEGORY' ) ) {
            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() ) ),
                __( 'Read more', 'generatepress' )
            );
    	}
        return $more;
    }, 50 );

    But does not work.
    Thanks.

    The page I need help with: [log in to see the link]

Viewing 15 replies - 1 through 15 (of 22 total)
  • You are supposed to use the category URL, not the category ID. Copy Paste the code here and then change the ‘slug’ part to the URL of your category page e.g. if your category is example.com/cats, then you cats is the category name or if it is example.com/cats-and-dogs, then cats-and-dogs is the category name. And of course, don’t forget to replace the Custom Text to your own.

    Thread Starter FeliceAntonio

    (@antonio_09)

    Thanks for your time.
    I yet tried too with the slug instead of the ID, the code is right, but I’m searching to understand why does not work, it is ignored.
    I wonder why, perhaps a sort of conflict in code of my file, but I don’t detect any error.

    Leo

    (@leohsiang)

    Can you copy and paste the exact code you are using here?

    Thread Starter FeliceAntonio

    (@antonio_09)

    My need is to set Read more in one category archive page.
    I found your code here, and I use in this way:

    add_filter( 'option_generate_blog_settings', 'lh_custom_read_more_text' );
    function lh_custom_read_more_text( $options ) {
        if ( is_category( 'NUMBER_CATEGORY' ) ) {
    	$options['read_more'] = 'Read more';
        }
      
        return $options;
    }

    Thanks.

    Leo

    (@leohsiang)

    That code would only work if you are using GP Premium.

    Without GP Premium, you would actually need the generate_excerpt_more_output code you posted in the original topic.

    Thread Starter FeliceAntonio

    (@antonio_09)

    That code would only work if you are using GP Premium -:)
    Sorry.
    I tried even this:

    add_filter( 'generate_excerpt_more_output', 'lh_custom_read_more_text' );
    function lh_custom_read_more_text( $options ) {
        if ( is_category( '1' ) ) {
    	$options['read_more'] = 'Read more';
        }
      
        return $options;
    }

    but it does not work.

    Theme Author Tom

    (@edge22)

    You would have to do this:

    add_filter( 'generate_excerpt_more_output ', function() {
        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() ) ),
    	__( 'Read more', 'generatepress' )
        );
    } );

    Then just change the text as needed 🙂

    @edge22 he just wants to change for one category. So adding a conditional tag to your code is the correct solution.

    Theme Author Tom

    (@edge22)

    Gotcha, so you’d do this:

    add_filter( 'generate_excerpt_more_output ', function( $output ) {
        if ( is_category( 123 ) ) {
            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() ) ),
    	    __( 'Read more', 'generatepress' )
            );
        }
    
        return $output;
    } );
    Thread Starter FeliceAntonio

    (@antonio_09)

    Thanks to everyone.
    I yet tried a solution like this, with conditional tag, but it didn’t work.
    I think there is a kind of conflict in translation.
    When I tried this code:

    add_filter( 'generate_excerpt_more_output', 'lh_custom_read_more_text' );
    function lh_custom_read_more_text( $options ) {
        if ( is_category( '1' ) ) {
    	$options['read_more'] = 'Read more';
        }
      
        return $options;
    }

    It works, but instead of Read more, returns ...altro (…other)
    Otherwise, always returns Leggi tutto translation of Read more.

    Leo

    (@leohsiang)

    Can you try only Tom’s solution?
    https://wordpress.org/support/topic/change-read-more-text-in-one-category/#post-13464748

    Make sure to replace 123 with the actual ID of the category.

    Thread Starter FeliceAntonio

    (@antonio_09)

    Yes, I tried, and changed the number category, but always returns Leggi tutto, Italian version translation of Read more.

    Thread Starter FeliceAntonio

    (@antonio_09)

    Thanks to everyone, as always, for the great and quick support.
    Every now and then, the programming language is like a voodoo (!!!) when seems right but there is something that escapes!
    I will come back on functions.php of my child theme to look for the bug.
    I’ll keep you up-to-date -:)

    Theme Author Tom

    (@edge22)

    Just to confirm, did you replace this line?: __( 'Read more', 'generatepress' )

    You should be able to do this:

    add_filter( 'generate_excerpt_more_output ', function( $output ) {
        if ( is_category( 123 ) ) {
            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() ) ),
    	    'YOUR NEW TEXT HERE'
            );
        }
    
        return $output;
    } );
    Thread Starter FeliceAntonio

    (@antonio_09)

    Well, I tried it now, but nothing to do.
    On the English pages, I would have to keep Read more, instead of the translation Leggi tutto.

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Change Read more text in one category’ is closed to new replies.