When we are creating a theme, we sometimes require multiple custom excerpts in a theme. but when ever we use add_filter
it affects all the the_excerpt
in a theme. So today I will give you a solution to How to Create multiple Excerpts in WordPress
What we are doing today are going to do is a simple solution of creating a custom function for the_excerpt
. which we will call out when ever we need excerpts of various sizes (Different Lengths). so for the beginning copy the below code in the functions.php
:
// Create the Custom Excerpts callback function wpden_excerpt($length_callback = '', $more_callback = '') { global $post; if (function_exists($length_callback)) { add_filter('excerpt_length', $length_callback); } if (function_exists($more_callback)) { add_filter('excerpt_more', $more_callback); } $output = get_the_excerpt(); $output = apply_filters('wptexturize', $output); $output = apply_filters('convert_chars', $output); $output = '<p>' . $output . '</p>'; echo $output; } // Custom Length function wpden_mag_len($length) { return 200; } |
once you have saved the above code in functions.php
you can call custom excerpt like this :
<?php wpden_excerpt('wpden_mag_len'); ?> |
Now you can use Custom Excerpt what ever times you want, just copy and paste Custom Length function and call it using Custom Excerpt. Also note that this Custom function also supports <!-- more -->
Tag so you can even customize Read More by Following code :
function wpden_more_view($more){ global $post; return '... <a class="view-article" href="' . get_permalink($post->ID) . '">' . __('View Article', 'wpden') . '</a>'; } |
you can call out it together with custom excerpt like showed in code below :
<?php wpden_excerpt('wpden_mag_len','wpden_more_view'); ?> |
So now you don’t need to use filters and can make as many modifications you like to the_excerpt
and Create Multiple Excerpts to your liking for theme you are making.
For More information on the_excerpt
visit it on WordPress Codex.