0

I'm developing a website that has a blog section as well as a "free monthly download" section set up as a custom post type. On the home page I'm displaying excerpts with "read more" links for both the most recent blog post and the most recent monthly download (these are in separate loops).

The read more link is generated automatically by this code in my functions.php file:

function excerpt_read_more_link($output) {
global $post;
return $output . '<a class="more-link" href="'. get_permalink($post->ID) . '">Read more</a>';
}
add_filter('the_excerpt', 'excerpt_read_more_link');

This is working fine, but I'd like "read more" for the custom post type excerpt to read "Get it now".

Any suggestions for how to have different "read more" text for the blog post excerpts and the custom post type excerpts?

Thanks in advance.

2 Answers 2

2

Hmm, and what is the difficulty in here? You already have global $post variable in your function. Just use it.

function excerpt_read_more_link($output) {
    global $post;
    $text = 'Read more';
    if ( $post->post_type == 'MY-CUSTOM-POST-TYPE' )  // 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');

Of course you can put multiple such if statements in there.

0
1

This will use "Get it now" for the link if the post is a 'my_custom_post_type' and use "Read more" if it is any other type of post.

function excerpt_read_more_link($output) {
    global $post;
    if ($post->post_type == 'my_custom_post_type')
        $read_more_text = 'Get it now';
    else
        $read_more_text = 'Read more';
    return $output . '<a class="more-link" href="'. get_permalink($post->ID) . '">'.$read_more_text.'</a>';
}
add_filter('the_excerpt', 'excerpt_read_more_link');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.