Using Multiple Excerpts Length in WordPress

Excerpt length plays an important role to the visitors plus to the visual look of the website. You can easily specify an excerpt length in the wordpress, since adding read more manually will not assure the beauty of the theme, since the homepage posts layout never shows a symmetry with the custom read more links in wordpress. If you are not aware that what is the excerpt length and how can you customize it then read this: Customizing Excerpt Length in WordPress

Using Multiple Excerpt Lengths in WordPress:

I am sure that most of the people will want the multiple excerpts length in wordpress since most of the times developers want to query posts from different categories with different excerpt length, and in the case there applies a universal excerpt length which is often annoying, as I showed you a way in the previous post that how can you add a custom excerpt length filter in wordpress.

Method 1: Using the Substitute String PHP Function:

There are two ways to add the multiple excerpts length support in wordpress, one way is probably the simplest and will not weigh much on the server. I am going to use a simple PHP function known as “substr” using which I’ll be able to select the number of characters from the main defined excerpt length in theĀ function.php. The code to be used in the loop for using another excerpt length suing substr php function is:

<?php echo substr(get_the_excerpt(), 0,120) . "..."; ?>

Thus, the above code selects the character from 0 to 120 from the original excerpt length. And in the end I catenated some three dots with the excerpt. Use the above code anywhere in the loop and you will be done! Just change the number 120 to something smaller or bigger as desired.

Method 2: Using the Function.php and Custom Defined Multiple Excerpts Called Through Main Excerpt:

Add the below function to the functions.php using the FTP and a good text editor.

function tow_excerpt($charlength) {
$excerpt = get_the_excerpt();
if(strlen($excerpt)>$charlength) {
$subex = substr($excerpt,0,$charlength-5);
$exwords = explode(" ",$subex);
$excut = -(strlen($exwords[count($exwords)-1]));
if($excut<0) {
echo substr($subex,0,$excut);
} else {
echo $subex;
echo "[...]";
} else {
echo $excerpt;

Be sure that your main excerpt length defined by the function.php is of greater number of characters, since the above code will strip the length from the main excerpt length, the main excerpt length can be defined easily by reading the guide in this post. So, i suppose that you have used a custom excerpt length of 100 characters now you can strip out 25 characters using the below code with the help of above function. Use the below code anywhere in the loop:

<?php echo tow_excerpt(25); ?>

Hope this will help you a lot while developing beautiful themes for wordpress.