Enquiry Email Form

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.

Learning & sharing

This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.

Please feel free to contribute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.



X