You could try something like the second option, but I think it would be overkill. Instead, just write your own custom analog to wp_trim_excerpt()
(the function that applies the excerpt_length
and excerpt_more
filters to the excerpt), like so:
function custom_excerpt( $new_length = 20, $new_more="...", $strip = false ) {
// Start with the content
$text = get_the_content('');
// Do stuff to it
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]>', $text);
// Use custom values
$excerpt_length = $new_length;
$excerpt_more = $new_more;
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
// Strip?
if ( ! $strip ) {
$text="<p>" . $text . '</p>';
}
// Output
echo $text;
}
Overkill method:
function custom_excerpt( $new_length = 20, $new_more="...", $strip = false ) {
// Excerpt length
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
function custom_excerpt_length( $new_length ) {
return $new_length;
}
// Excerpt More
add_filter( 'excerpt_more', 'custom_excerpt_more' );
function custom_excerpt_more( $new_more ) {
return $new_more;
}
// Output
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
if(!$strip)
$output="<p>" . $output . '</p>';
echo $output;
}