Skip to content

Instantly share code, notes, and snippets.

@jakefentress
Created May 19, 2014 18:59
Show Gist options
  • Save jakefentress/5e300e420d61a9a72f37 to your computer and use it in GitHub Desktop.
Save jakefentress/5e300e420d61a9a72f37 to your computer and use it in GitHub Desktop.
Excerpt Options for WordPress
// replace some text in the returned excerpt
function excerpt_no_podcast($output) {
global $post;
return str_replace('Podcast: Play in new window | Download ', '', $output);
}
add_filter('the_excerpt', 'excerpt_no_podcast');
// it can also be used to add a "read more" link for the manually created excerpt
function excerpt_read_more_link($output) {
global $post;
return str_replace('</p>', ' <a href="'. get_permalink( get_the_ID() ) . '">Read More&nbsp;&raquo;</a></p>', $output);
}
add_filter('the_excerpt', 'excerpt_read_more_link');
// though you may want to account to excerpts with multiple paragraphs
function excerpt_read_more_link($output) {
global $post;
return preg_replace('#<\/p>(?!.*<\/p>)#s', ' &#8230; <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Continue Reading</a></p>', $output);
}
add_filter('the_excerpt', 'excerpt_read_more_link');
// to create a "Read More" link for the automatically created excerpt, do this:
function new_excerpt_more( $more ) {
return ' <a href="'. get_permalink( get_the_ID() ) . '">Read More&nbsp;&raquo;</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
// if you have a mix of manually and automatically generated excerpts, do this to get rid of the automatic ellipses:
function new_excerpt_more( $more ) {
return '';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );
// if you want to remove the <p> that is added around every excerpt, use this:
remove_filter('the_excerpt', 'wpautop');
// if you do this, you may want to change how the "read more" is created. Instead of doing a string replace, we are adding the read more to the end of the excerpt string.
function excerpt_read_more($output) {
global $post;
return $output.' &#8230; <a class=="read-more" href="'. get_permalink( get_the_ID() ) . '">Continue Reading</a>';
}
add_filter('the_excerpt', 'excerpt_read_more');
// change the length of the excerpt (this sets the number of words)
function custom_excerpt_length( $length ) {
return 24;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
// if you want to do something with the excerpt only for the homepage, do something like this:
function excerpt_read_more($output) {
global $post;
if (is_front_page()) {
return preg_replace('#<\/p>(?!.*<\/p>)#s', ' &#8230; <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Continue Reading</a></p>', $output);
} else {
return $output;
}
}
add_filter('the_excerpt', 'excerpt_read_more');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment