wordpress excerpt length for custom post type
If you are a WordPress user, then you know that in WordPress there are many pre-defined functions that automate a lot of the features that dictate how your content on the frontend is displayed. A common example is how your posts loop display excerpts of your articles.
But do you know that if you want, then you can easily customize the WordPress excerpt length and content length? In this tutorial, we will explain to you how you can easily extend the limit of your article excerpt length or your custom post-type excerpt length!
What is the Excerpt in WordPress?
In WordPress excerpt is is a short description or summary of the post or article that we show to readers to get their attention in reading the article and makes them want to read further it. Because sometimes a title may not be enough to get your audience’s attention, neither it convey the quality of article content that well. You can use excerpt to display post summary on blog page and search page, in the homepage, and in the widgets where your posts are placed. You can use the_excerpt() function to display the post excerpt of your article.
How to Change the Excerpt Length?
To change excerpt length, add the following code to in functions.php
file of your theme.
<?php /** * WP Custom Excerpt Length Function * Place in functions.php * This example returns ten words, then [...] * Manual excerpts will override this */ function wp_custom_excerpt_length( $length ) { return 10; } add_filter( 'excerpt_length', 'wp_custom_excerpt_length', 999 ); ?>
You might be want to display the post excerpt of different post types based on the layout of displaying posts. You can use the below function to define the different post excerpt length for different post types.
<?php /** * WP Custom Excerpt Length Function * Place in functions.php * This example returns ten words, then [...] * Manual excerpts will override this */ function wp_custom_excerpt_length($length) { global $post; if ($post->post_type == 'post') return 25; else if ($post->post_type == 'portfolio') return 15; else return 25; } add_filter('excerpt_length', 'wp_custom_excerpt_length'); ?>
That’s it. I hope you have find this article useful.