Adding Custom Excerpt Lengths in WordPress: A Guide

link. This allows for greater flexibility and customization for the end user.

Customizing Excerpts in WordPress

WordPress is a popular content management system that powers millions of websites around the world. One of its key features is the ability to display excerpts of posts or pages on your website. By default, WordPress excerpts are set to 55 words, but what if you wanted to display different excerpt lengths on different sections of your site? For example, if you are developing a website with multiple post types, you may want to show different excerpt lengths depending on the post type. In this article, we will show you how to customize excerpts in WordPress using a custom function.

Custom Excerpt Function

Below is a custom function that we have developed to help you display excerpts at different lengths in WordPress. To use the function, simply add it to your functions.php file and then replace either the_excerpt or the_content functions throughout your theme so you can have different excerpt lengths.

function wpex_get_excerpt( $args = array() ) {

// Default arguments.

$defaults = array(

‘post’ => ”,

‘length’ => 40,

‘readmore’ => false,

‘readmore_text’ => esc_html__( ‘read more’, ‘text-domain’ ),

‘readmore_after’ => ”,

‘custom_excerpts’ => true,

‘disable_more’ => false,

);

// Apply filters to allow child themes mods.

$args = apply_filters( ‘wpex_excerpt_defaults’, $defaults );

// Parse arguments, takes the function arguments and combines them with the defaults.

$args = wp_parse_args( $args, $defaults );

// Apply filters to allow child themes mods.

$args = apply_filters( ‘wpex_excerpt_args’, $args );

// Extract arguments to make it easier to use below.

extract( $args );

// Get the current post.

$post = get_post( $post );

// Get the current post id.

$post_id = $post->ID;

// Check for custom excerpts.

if ( $custom_excerpts && has_excerpt( $post_id ) ) {

$output = $post->post_excerpt;

}

// No custom excerpt…so lets generate one.

else {

// Create the readmore link.

$readmore_link = ‘‘ . $readmore_text . $readmore_after . ‘‘;

// Check for more tag and return content if it exists.

if ( ! $disable_more && strpos( $post->post_content, ‘‘ ) ) {

$output = apply_filters( ‘the_content’, get_the_content( $readmore_text . $readmore_after ) );

}

// No more tag defined so generate excerpt using wp_trim_words.

else {

// Generate an excerpt from the post content.

$output = wp_trim_words( strip_shortcodes( $post->post_content ), $length );

// Add the readmore text to the excerpt if enabled.

if ( $readmore ) {

$output .= apply_filters( ‘wpex_readmore_link’, $readmore_link );

}

}

}

// Apply filters and return the excerpt.

return apply_filters( ‘wpex_excerpt’, $output );

}

How to Use the Function

Now, instead of using “the_excerpt()” in your loops, you can use “wpex_excerpt( $args )”, where $args holds an array of your preferred settings for the specific excerpt. For example:

‘length’ => 40,

‘readmore’ => true,

‘readmore_text’ => esc_html__( ‘read more’, ‘wpex-boutique’ ),

‘custom_excerpts’ => true,

) ); ?>

Function Filters

You may notice that our snippet includes various apply_filters() functions. The reason for this is if you are working on a premium or free theme or plugin for distribution, you will want to give the end-users the ability to alter their excerpts via their child theme if needed. For example, if you defined an excerpt to be a certain length for a section of the site, the end-user could always use the “wpex_get_excerpt_args” filter to change the excerpt to a different length or customize the text or readmore link. This allows for greater flexibility and customization for the end-user.

Conclusion

Customizing excerpts in WordPress is a great way to make your website stand out and provide a better user experience. With the custom function we’ve provided, you can easily display excerpts at different lengths throughout your site. By using filters, you can give your end-users the ability to customize their excerpts to their liking. We hope this article has been helpful in showing you how to customize excerpts in WordPress.

Stay in Touch

spot_img

Related Articles