Writing a custom excerpt function

I want this because:

1-) I have a list item ol /ol at the start of my every post.

2-) When wordpress shows summary of the post it reads some words from the beginning of the post.

3-) This causes the summary to be comprised of the navigation list of the post. The words from inside the ol /ol tags.

4-) I don’t want the words from ol tags in my excerpt. I want it to directly go beyond the /ol tags for the excerpt content.

My code is:

function custom_excerpt($text) {
    $pos = strpos($text, '</ol>');
    $text = substr($text, $pos);
    return $text;
}
add_filter('the_excerpt', 'custom_excerpt');

This doesn’t work. I guess the tags are already stripped so “strpos” can’t find anything. But even i return “…” from function, the same text as before stays. Like my custom function has no effect. Why is that? Is the theme’s functions somehow interferes with the excerpt code?

1 Answer
1

The easiest way is to use strip_tags().

// Remove all html from the excerpt.
function custom_excerpt( $text ) {
    return strip_tags( $text );
}
add_filter( 'the_excerpt', 'custom_excerpt' );

If you want to get fancy and trim the excerpt length, you can use this function.

// Trim default excerpt length from 55 chars to 20. 
function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 99 );

Leave a Comment