excerpt length filter wordpress code example
Example: wordpress change excerpt length
function wpex_get_excerpt( $args = array() ) {
$defaults = array(
'post' => '',
'length' => 40,
'readmore' => false,
'readmore_text' => esc_html__( 'read more', 'text-domain' ),
'readmore_after' => '',
'custom_excerpts' => true,
'disable_more' => false,
);
$defaults = apply_filters( 'wpex_get_excerpt_defaults', $defaults );
$args = wp_parse_args( $args, $defaults );
$args = apply_filters( 'wpex_get_excerpt_args', $defaults );
extract( $args );
if ( ! $post ) {
global $post;
}
$post_id = $post->ID;
if ( $custom_excerpts && has_excerpt( $post_id ) ) {
$output = $post->post_excerpt;
}
else {
$readmore_link = '<a href="' . get_permalink( $post_id ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>';
if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) {
$output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) );
}
else {
$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );
if ( $readmore ) {
$output .= apply_filters( 'wpex_readmore_link', $readmore_link );
}
}
}
return apply_filters( 'wpex_get_excerpt', $output );
}