Here is how to setup a custom WordPress excerpt function with the following criteria:

  • This will allow you to limit the amount of characters displayed in the excerpt
  • While limiting the number of characters displayed, this will also prevent the last word from getting cut off.  In other words, it will limit the number of characters but fully display the last word
  • Show “read more” or “…” text after the excerpt

Here is how this is done.  First, put the following snippet of code in your functions.php file:


// Filter excerpt length to 25 words.
function mwx_custom_excerpt_length( $length ) {
return 25;
}
add_filter( 'excerpt_length', 'mwx_custom_excerpt_length', 999 );
 

The section above that says return 25 is where you would set the word limit by replacing 25 with your desired number of words.

Next, you can call the excerpt in your theme file using this code:


<?php the_excerpt();?>

Leave a Reply

Get in Touch

Please fill out the form below and we will get back to you ASAP

  • This field is for validation purposes and should be left unchanged.