2

Is there an easy way to add another 'read more' link? I'm using the standard 'continue reading' link for some posts but I also want to use a different text like 'more info' for other posts. Can I do this using the_excerpt?

Later update: This are the two standard twentyelven functions that are used for the 'read more' or 'continue reading' links.

function twentyeleven_continue_reading_link() {
    return ' <a href="'. esc_url( get_permalink() ) . '">' . __( '<span class="more">more &#43;</span>', 'twentyeleven' ) . '</a>';
}
function twentyeleven_auto_excerpt_more( $more ) {  
    return ' &hellip;' . twentyeleven_continue_reading_link();
}
add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );

Based on amit code I though about including an if / else statement in the twentyeleven_auto_excerpt_more function, but I did something wrong because it doesn't work.

This is the code:

function twentyeleven_auto_excerpt_more( $more ) {
$read_more_link_2 = "View Image";
if ( in_category( 'events' )) {
    return $read_more_link_2;
} else {    
    return ' &hellip;' . twentyeleven_continue_reading_link();
}
add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );

I think it's obvious that PHP is not my strong point.

3
  • Will you update question with the code you're using to print read more link ?
    – Amit Kosti
    Commented Jul 30, 2012 at 18:31
  • I did, and I've included the code you provided, but there's something wrong with the way I did it because it's not working. Commented Jul 30, 2012 at 20:52
  • I've udated my answer to work with twenty eleven theme.
    – Amit Kosti
    Commented Jul 30, 2012 at 21:58

2 Answers 2

2

How about using condition to change the readmore links.

Here is an sample code which returns a different read more text based upon the category of post. You should read the official codex page to know more about other conditional tags you can use in wordpress.

Usage - put this code into your theme's functions.php file. This code will replace the continue reading with View Image if post is in image category.

<?php
        function wpse_60184_new_readmore_link( $more ) {
                $read_more_link_2 = "View Image";
                if ( in_category( 'image' )) {
                        return $read_more_link_2;
                }
        }
        add_filter('excerpt_more', 'wpse_60184_new_readmore_link');
?>

Update #1

Put this code at the end of your theme's functions.php

//this will create read morelink when the_excerpt() is used
function wpse_60184_the_excerpt_more($more) {
    in_category( 'events' ) ? $my_read_more_text = 'Read event' : $my_read_more_text = 'Read more'; 
    global $post;
    return '<a href="'. get_permalink($post->ID) . '">'.$my_read_more_text.'</a>';
}
add_filter('excerpt_more', 'wpse_60184_the_excerpt_more');

//this will change the read more link when <!-- more --> is used & the_content()       
function wpse_60184_the_content_more( $more_link, $more_link_text ) {
    in_category( 'events' ) ? $my_read_more_text = 'Read events' : $my_read_more_text = 'Read more';    
    return str_replace( $more_link_text, $my_read_more_text, $more_link );
}
add_filter( 'the_content_more_link', 'wpse_60184_the_content_more', 10, 2 );

The second code is tested with twenty eleven theme and it appears to be working fine on localhost.

  • wpse_60184_the_excerpt_more creates a read-more link when the_excerpt() is used in theme
  • wpse_60184_the_content_more will replace the default read more link with new one. Will work with <!-- more --> tag.
1

You can use the excerpt_more filter hook.

I suggest just adding a specific meta data field to posts that this filter would then use to overwrite the default text.

Possible PHP could be as follows (untested):

function custom_excerpt_more( $more ) {
    global $post;

    $custom_read_more = get_post_meta($post->ID, 'read_more_text', true);
    if (!empty($custom_read_more))
        return $custom_read_more;
    else
        return $more;
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );

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.