Programming

WordPress, Designing Custom Templates: Individual Lengths for Article Excerpts

1 minute read

This post is from May 2012. The screenshots show the interface as it was then; today it looks slightly different in places.

On the article page (index), one might want to adjust the length of the excerpt. However, on another page, one might also desire a preview—for instance, the 3 latest posts—with a more concise version of the article excerpt.

For the first scenario, only this entry in `functions.php` is required:

function custom_excerpt_length( $length ) {
  // Standard ist 54
  return 40;  
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Problem: This sets all excerpts to 40 words in length.

To differentiate, for example, based on the page being visited, one can simply query the page within the function.

function custom_excerpt_length( $length ) {

  // Home ist index
  if( is_home() ){
    return 40;
  }else{
    return 10;
  }
}

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );