Enquire

WordPress- Custom Excerpt Length

Wordpress Tips & Code Snippets

Here’s a bit of code that allows you to to create a custom excerpt length, based on the amount of characters you want to display.

Code

Add the following code into the functions.php located in the theme directory.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function new_excerpt($charlength) {
 $excerpt = get_the_content();
 $charlength++;
 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;
  }
}

Usage

You can now replace the_excerpt() or the_content() function with the new function

1
2
3
4
5
//old
<?php the_excerpt(); ?>
 
//new
<?php new_excerpt(30); ?>

Change the value 30 to how many characters http://essaywritingservices.com.au/ you want displayed in your excerpt.

Comments

Leave a reply

Your email address will not be published. Required fields are marked *

X