Site logo

Archived topic

Customizing the "read more" text on all archive pages

24 replies · Started by Bandara on May 9, 2021

Viewing posts 1–15 of 25

Hello!

I'm using the code on this page to always force having the "read more" button on my archive pages: https://docs.generatepress.com/article/activating-read-custom-excerpt/

Now I would like to be able to customize the text of this button based on custom post types and maybe categories if possible.

I found this code

function excerpt_read_more_link($output) {
    global $post;
    $text = 'Read more';
    if ( $post->post_type == 'podcast' )  // change MY-CUSTOM-POST-TYPE to your real CPT name
        $text = 'Get it now';
    return $output . '<a class="more-link" href="'. get_permalink($post->ID) . '">'. $text .'</a>';
}
add_filter('the_excerpt', 'excerpt_read_more_link');

however instead of changing the text of the existing read more button, it puts a new link below the button.

So first question is how to get this functionality but change the existing button. (Of course I don't need to use the code I pasted above)

Second question, if I use that code, can another if statement be added to select for categories (of the built in posts, not custom post types) so I could change the text based on categories?

I've been trying to figure this out on my own but am stumped. Thanks!

I'm still working on this, but not making any progress. I'm guessing that the_excerpt isn't the proper way to modify this in GP, but I can't figure out how to do it.

Hi there,

This one totally slipped from us. My apologies.

IF you're trying to apply this on an archive page, don't forget to include is_archive() on the if conditions.

Example:

if ( is_archive() && $post->post_type == 'podcast' ) { // change MY-CUSTOM-POST-TYPE to your real CPT name
        $text = 'Get it now';
    return $output . '<a>ID) . '">'. $text .'</a>';
}

Hmm. Still doesn't work. To clarify, I'm testing on a tag archive page. Here is the code as it stands now:

/*customize read more for podcast*/

function excerpt_read_more_link($output) {
    global $post;
    $text = 'Read more';
    if ( is_archive() && $post->post_type == 'podcast' )  // change MY-CUSTOM-POST-TYPE to your real CPT name
        $text = 'Listen now';
    return $output . '<a class="more-link" href="'. get_permalink($post->ID) . '">'. $text .'</a>';
}
add_filter('the_excerpt', 'excerpt_read_more_link');

It works in the sense that the Listen now appears but it appears in addition to and below the button.

Consider trying this approach:

add_filter( 'option_generate_blog_settings', 'lh_custom_search_results_page_settings' );
function lh_custom_search_results_page_settings( $options ) {
    if ( is_archive() && $post->post_type == 'podcast' ) {
    $options['read_more'] = 'Listen now'
    }
  
    return $options;
}

Hmm. I'm getting this error:

Parse error: syntax error, unexpected '}' in /home/myfolder/local.mysite.org/wp-content/themes/generatepress_child/functions.php on line 203

That line is

200 function lh_custom_search_results_page_settings( $options ) {
 201   if ( is_archive() && $post->post_type == 'podcast' ) {
 202   $options['read_more'] = 'Listen now'
 203   }

I tried putting a ; at the end of

202 $options['read_more'] = 'Listen now'

But then I get this error:

Fatal error: Cannot redeclare lh_custom_search_results_page_settings() (previously declared in /home/myfolder/local.mysite.org//wp-content/themes/generatepress_child/functions.php:56) in /home/myfolder/local.mysite.org/wp-content/themes/generatepress_child/functions.php on line 200

Once we get it working, I'm wondering if there is a way to modify this $post->post_type == 'podcast' to test for categories? would I change post_type to post_category?

Ah yeah I missed that delimiter. My bad.

And I forgot to change the condition. We're not calling global $post so we should change it.

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

Once we get it working, I’m wondering if there is a way to modify this $post->post_type == 'podcast' to test for categories? would I change post_type to post_category?

Sure. use is_category('your-category-slug') if it's an category archive.

If it's for a post with a specific category, try has_category() or in_category()

Hmm. Still getting an error. I don't believe there is caching on this sandbox site.

Fatal error: Cannot redeclare lh_custom_search_results_page_settings() (previously declared in /myfolder/local.mysite.org/wp-content/themes/generatepress_child/functions.php:56) in /home/myfolder/local.mysite.org/wp-content/themes/generatepress_child/functions.php on line 200

Fatal error: Cannot redeclare lh_custom_search_results_page_settings()

Don't keep in 2 functions of the same name at the same time.

If you've added the first one I've given, replace it with the newer code I've provided instead of keeping both active.

Don’t keep in 2 functions of the same name at the same time.

Oddly, I didn't have it there. The function name was only in the file twice, once for the declaration and another for the add filter. I changed it to something completely different and now it works. Yay! Thanks.

So I'm trying to get the category thing working. I'm hoping that I can affect both in category and tag archives. At the moment I can't get it working in either. I must not have included your mod correctly. Here is what I have now:

/*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 now';
    }
	if ( is_archive() && is_category('mirror-of-dhamma') ) {
    $options['read_more'] = 'Watch';
    }
  
    return $options;
}

I have tried is_category, in_category, has_category. Nothing works. I assumed it returns a boolean value. mirror-of-dhamma is the category slug.

To clarify, the posts I am trying to target are just regular posts, not a custom post type.

Sorry to bother you so much.

Hmm...

I have also tried this

if ( is_archive() && in_category('mirror-of-dhamma', get_the_ID() ) )

But even this doesn't work.

Any ideas?

is_archive() && is_category('mirror-of-dhamma')

What did you want to achieve with this?

is_category() is almost the same as is_archive() except it checks category archive pages only.

Oh, ok.

So right now the category and tag taxonomies are shared by regular posts and the podcast CPT.

On tag and category archive pages (and even search pages would be great too) I want the "Read More" button to actually reflect the nature of the content.

So, as we have achieved, podcast CPT button should be "Listen". That's perfect now.

For posts categorized as videos, it should be "Watch".
For posts categorized as Audio, it should be "Listen".
And the rest of the posts we can assume should be the normal "Read More"

So the end result will be that tag archives will show a mixture of content types (all from either the standard posts or the podcast cpt) and the buttons will reflect that (Listen, Watch, Read) based on the category.

No, that still doesn't do it. This is the whole function:

/*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';
    }
	if ( has_category('mirror-of-dhamma') ) {
    $options['read_more'] = 'Watch';
}
  
    return $options;
}

I'm sure that the function is being used because I changed the button text again for the podcast cpt and indeed that changed.

This archived topic is closed to new replies.