WordPress excerpts are among those fundamental parts of creating a wordpress based website. Excerpts are essential to those who want to show a snippet of the text of a new blog post.

 

Excerpts may be shown on the homepage of a website, on the archives page, on a sidebar and much more, but when defining an excerpt, you define the text length. This is fine if you wish all excerpts are of the same length, but how do you create multiple excerpts if you want multiple lengths.

 

The answer stands below, and once studied, it becomes very clear. This code needs to be placed in functions.php.

 

function excerpt_index( $length ) {return 22;}
function excerpt_blog ( $length ) {return 50;}
function excerpt_more_arrow( $more ) {return ' <a href="'. get_permalink($post->ID) . '">→</a>';}

function my_excerpt( $length_callback = '', $more_callback = '' ) {

    if (function_exists($length_callback)) add_filter( 'excerpt_length', $length_callback );
    if (function_exists($more_callback)) add_filter( 'excerpt_more', $more_callback );

    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    $output = '
' . $output . '

';
    echo $output;
}

 

*wptexturize, returns the given text with transformations of quotes to smart quotes, this can be disabled by using remove_filter(‘the_excerpt’, ‘wptexturize’);.
 
*convert chars, converts the number of characters from the string.
 
The functions at the being of the code, defines the length of the specific excerpts, once defined the below piece of php code needs to be inserted in the correct position in specific template, for example archives.
 

 <!--?php my_excerpt('excerpt_blog', 'excerpt_more_arrow'); ?-->

 
This code was originally sourced from wpengineer and then adjusted.

© Copyright of Alexandra Milne 2013 | Privacy Policy | Disclaimer | Disclosure