In WordPress, the excerpt is a summary of the post’s content that is often used in archive pages or on the homepage. You can add different excerpt formats to different templates by using the the_excerpt() function and modifying it to suit your needs.

  1. Create a new template file and add the the_excerpt() function to it.
  2. Modify the excerpt_length and excerpt_more filters to change the length and formatting of the excerpt.
function custom_excerpt_length( $length ) {
    return 25;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

function custom_excerpt_more( $more ) {
    return '...';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );

3. You can also create a new custom function to show the excerpt and then use it in your template file.

function custom_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" ([.*?])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 150);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    return $excerpt;
}
4. You can also use the wp_trim_words() function instead of creating a custom function.
$excerpt = wp_trim_words(get_the_content(), 25, '...');

You can then use the function in your template file where you want to show the excerpt.

Note that the above code snippets are examples and you may need to adjust them to suit your specific needs and requirements.

It’s also worth noting that you can use Advanced Custom Fields plugin to create custom fields in your single post template and use them in your excerpt.

(Visited 25 times, 1 visits today)
Was this article helpful?
YesNo
Close Search Window