Site logo

Archived topic

Customizing the "read more" text on all archive pages

24 replies · Started by Bandara on May 9, 2021

Viewing posts 16–25 of 25

That's strange.

The same code works on my end.

I used this condition:

if ( has_category('featured') ) {
    $options['read_more'] = 'Watch';
}

I had a category with a slug of featured which is a child category of parent and it still worked.

Can you show me which list are you checking this on? Perhaps you're using a custom WP_query from some plugin. The snippet won't work on that.

Can you show me which list are you checking this on?

Not sure I know what you mean by list. In my previous post private comment I linked to the tag archive I was using to check.

Perhaps you’re using a custom WP_query from some plugin. The snippet won’t work on that.

I'm just trying to get this to work on the built in tag and category archive pages.

So, I'm still using the code that I linked to in my original post. This code:

/*always have read more link*/
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;

    if ( has_excerpt() ) {
        $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
            $excerpt,
            get_permalink(),
            __( 'Read more', 'generatepress' )
        );
    }
	
    return $output;
}

Because without that no button shows at all when I have a custom excerpt entered on a post.

So, yes this is the issue. I removed the custom excerpt from that post I was using to test and also removed the above code. And indeed now it works!

But the question is how to get the two to work together. Because I need to have custom excerpts as well has having the buttons.

Ah I see now.

What happens if you try this?

add_filter( 'gettext', function( $translated_text ) {
    if ( 'Read More' === $translated_text ) {
        if(has_category('mirror-of-dhamma')){
            $translated_text = 'Watch';
        }
    }

    return $translated_text;
} );

Ok, So here is everything I have so far:

/*always have read more link*/

add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
    $output = $excerpt;

    if ( has_excerpt() ) {
        $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
            $excerpt,
            get_permalink(),
            __( 'Read much more', 'generatepress' )
        );
    }
	
    return $output;
}

/*customize read more for podcast, categories, etc*/

add_filter( 'option_generate_blog_settings', 'sc_custom_read_more_buttons' );
function sc_custom_read_more_buttons( $options ) {
    if ( is_archive() && 'podcast' == get_post_type( get_the_ID() ) ) {
    $options['read_more'] = 'Listen right now';
    }
	
    return $options;
}

add_filter( 'gettext', function( $translated_text ) {
    if ( 'Read More' === $translated_text ) {
        if(has_category('mirror-of-dhamma')){
            $translated_text = 'Watch';
        }
    }

    return $translated_text;
} );

The code you gave doesn't make it work.

On the post with category mirror-of-dhamma...

—When there is a custom excerpt, it makes a button that says "Read much more" (I modified it to make sure I knew what code was causing it).

—When there is no custom excerpt, it makes a button that says "Read more".

To recap what I am trying to do...
#1 Whether there are custom excerpts or not, the button should show
#2 I want to have button text based on the CPT (working!) or the category (not working with #1)

Hi there, let's try this instead:

add_filter( 'wp_trim_excerpt', function( $excerpt ) {
    $output = $excerpt;

    $settings = wp_parse_args(
        get_option( 'generate_blog_settings', array() ),
        generate_blog_get_defaults()
    );

    if ( has_excerpt() ) {
        $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
            $excerpt,
            get_permalink(),
            $settings['read_more'] ? $settings['read_more'] : 'Read more'
        );
    }
	
    return $output;
} );

/*customize read more for podcast, categories, etc*/

add_filter( 'option_generate_blog_settings', function( $options ) {
    if ( is_archive() && 'podcast' == get_post_type( get_the_ID() ) ) {
        $options['read_more'] = 'Listen right now';
    }
	
    return $options;
} );

This makes it so the first function uses the value set in the Customizer, and the second function filters that value based on your conditions.

Let me know :)

Great! After I figured out what to put where, it's doing exactly what I need. Much thanks to both of you.

For anyone in the future looking at this thread, here is the code I ended up using:

/*always have read more link*/

add_filter( 'wp_trim_excerpt', function( $excerpt ) {
    $output = $excerpt;

    $settings = wp_parse_args(
        get_option( 'generate_blog_settings', array() ),
        generate_blog_get_defaults()
    );

    if ( has_excerpt() ) {
        $output = sprintf( '%1$s <p class="read-more-button-container"><a class="button" href="%2$s">%3$s</a></p>',
            $excerpt,
            get_permalink(),
            $settings['read_more'] ? $settings['read_more'] : 'Read more'
        );
    }
	
    return $output;
} );

/*customize read more for podcast, categories, etc*/

add_filter( 'option_generate_blog_settings', function( $options ) {
    if ( is_archive() && 'podcast' == get_post_type( get_the_ID() ) ) { //looks for CPT podcast
        $options['read_more'] = 'Listen';
    }
	
	if ( has_category(array('audio')) ) { //looks for category audio
        $options['read_more'] = 'Listen';
    }
 
	if ( has_category(array('mirror', 'short-sermons', 'lasting-happiness')) ) { // looks for any of the categories in the list
        $options['read_more'] = 'Watch';
    }
	
    return $options;
} );

The only thing I have left to do is to see if for the last condition I can automatically generate an array with all the child categories of a specific parent category. But I will take that to stackexchange since it's a general question.
Figured it out:

if ( has_category(get_terms( array('taxonomy' => 'category', 'parent' => 9, 'fields'    => 'id=>slug') )) ) {
        $options['read_more'] = 'Watch right now';
    }

Thanks again!

Glad we could help! Thanks for sharing your code :)

Thanks for sharing your code

Of course! We all know how frustrating it is to get to the end of a support post just to have the OP say simply, "Oh, I figured it out!" and then 🦗🦗🦗

This archived topic is closed to new replies.