Skip to content

Instantly share code, notes, and snippets.

@robertdevore
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robertdevore/388b64725c2a5d7fcd96 to your computer and use it in GitHub Desktop.
Save robertdevore/388b64725c2a5d7fcd96 to your computer and use it in GitHub Desktop.
WordPress: the_excerpt customizations
<?php
/**
* Changing the excerpt length
*
* the default length is 55 words and the code below changes it to 25 words
*/
function custom_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
/**
* Adding a 'Read More' link
*
* the code below removes the hideous '[...]' from the end of the_excerpt() and adds '... Read More'
*/
function custom_excerpt_more( $more ) {
return ' ... <a class="read-more" href="' . get_permalink( get_the_ID() ) . '">' . __( 'Read More', 'your-text-domain' ) . '</a>';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
/**
* Adding css class to <p> wrapper
*
* The code below adds the class 'excerpt' to the <p> tag that wraps around the excerpt
*/
function add_class_to_excerpt( $excerpt ) {
return str_replace('<p', '<p class="excerpt"', $excerpt);
}
add_filter( "the_excerpt", "add_class_to_excerpt" );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment