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 +</span>', 'twentyeleven' ) . '</a>';
}
function twentyeleven_auto_excerpt_more( $more ) {
return ' …' . 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 ' …' . twentyeleven_continue_reading_link();
}
add_filter( 'excerpt_more', 'twentyeleven_auto_excerpt_more' );
I think it’s obvious that PHP is not my strong point.
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.