Custom excerpt length & Read More link WordPress

Posted on 26th May, 2014 62 Comments

I often want to use a custom excerpt length in my themes. Custom excerpts can be really useful whether they are being used for the archives, home page or even when being used in meta description! Being able to add just the right amount of copy to a section is easy with this snippet.

New Excerpt Length

Functions.php

Add this to your functions.php:

/*
 * NEW EXCERPT LENGTH
 *************************************************************/
function content($limit) {
	$content = explode(' ', get_the_content(), $limit);
	  if (count($content)>=$limit) {
		  array_pop($content);
		  $content = implode(" ",$content).'…';
	  } else {
		  $content = implode(" ",$content);
	  }
		  $content = preg_replace('/[.+]/','', $content);
		  $content = apply_filters('the_content', $content);
		  $content = str_replace(']]>', ']]>', $content);
	return $content;
}

Then to display this excerpt in your theme add this:

// Change the '38' to which ever number of words you would like to display
<?php $content = get_the_content(); echo wp_trim_words( $content , '38' ); ?> 

If you would like to use this new excerpt for your page meta description then you may want to use this. In your header.php add your meta description with this.

	<?php if (is_single() || is_page() ) : if ( have_posts() ) : while ( have_posts() ) : the_post(); 
	// IF WE'RE ON THE SINGLE POST, A PAGE USE THE EXCERPT AS THE DESCRIPTION
	?>
         <meta name="description" content="<?php $content = get_the_content(); echo wp_trim_words( $content , '38' ); ?>" />
			 <?php endwhile; endif; elseif(is_home()) : 
	// IF WE'RE ON THE HOME PAGE THEN USE THE BLOG DESCRIPTION
	?>
         <meta name="description" content="<?php bloginfo('description'); ?>" />
			 <?php endif; ?>

Read More Links

If you’re adding a custom excerpt then why not add a nice new read-more link.

functions.php

/*
 * READ MORE
 *************************************************************/

function new_excerpt_more($more) {
global $post;
// This will automatically be added where the read-more link normally appears.
// Change the '<b>+</b> Read More' to your own message...
return '… <div class="read-more"><a class="button" href="'. get_permalink($post->ID) . '">' . '<b>+</b> Read More' . '</a></div>';
}
add_filter('excerpt_more', 'new_excerpt_more');

Comments

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]

Aaron

02nd, Feb, 15

Gary, I like your thinking. Good post mate 🙂

techno69

03rd, Jun, 15

Took me ages to find an article that explained this as easy as your, so thanks, hope to read more of your wordpress hacks.

JamesVen

20th, Oct, 20

the the lipid for nobles and cardiac flop, hypertension, posterior. [url=https://ciamedusa.com/#]where to buy cialis online[/url] generic cialis com Uncontrolled and Apology

RobertBAR

31st, Oct, 20

erectile linked diabetes [url=http://erectiledysfunctionpillscvs.com/#]help with erectile dysfunction[/url] erectile ring walmart

RobertBAR

05th, Nov, 20

erectile discomfort [url=http://erectiledysfunctionpillscvs.com/#]erectile pills without a doctor prescription[/url] erectile pill as shown on shark tank

HarryCloth

06th, Nov, 20

ohio warren county health department grease interceptor spec
[url=http://canadianpharmacy-md.com]canadian pharmacies[/url]

camo phone case

08th, Nov, 20

What’s up, this weekend is fastidious in support of
me, for the reason that this point in time i am reading this wonderful
educational paragraph here at my residence.

Stevewah

08th, Nov, 20

[url=https://sittindonesia.org/d6967fe9748d6dc60a6682f497bcfd1c/book/1603728917-9781874897750]The Diary of Thami the Truck Driver: Level 3[/url]

[url=https://serpillo.it/fc96fad52c0d859b1f88f6622317530d/magazine/t3-uk-march-2019]T3 UK – March 2019]

[url=https://mayxaydungsg.com/en/b2f7242ec1e43289269a9e92c73be0ae/stream/9503-1604255879/1984]Lace[/url]

[url=https://alufiero.it/en/f4e82d07710d7fc5bb4b7ba00308a0ae/stream/40205-1603688170/2010]16 Wishes]

rsasyw

09th, Nov, 20

arkansas department of health immunization exemption state of california health and human services department of social services
https://www.withoutdroch.com viagra without a doctor prescription [url=https://www.withoutdroch.com/]viagra without a doctor prescription[/url] adb50v

JesMox

14th, Nov, 20

Truly loads of helpful knowledge..
[url=https://www.cialiset.com]cialis generic[/url]

JosCance

14th, Nov, 20

what is the bottom number on blood pressure doctor practice near me
[url=https://viagrage.com]viagra without doctor prescription[/url] getting a viagra prescription http://viagrage.com

we just did 46 hat

16th, Nov, 20

This design is incredible! You definitely know
how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great
job. I really enjoyed what you had to say, and more than that,
how you presented it. Too cool!

EMaf

26th, Nov, 20

[url=https://www.cialis20walmart.com/]cialis generic[/url] [url=https://www.winviag.com/]generic viagra[/url] [url=https://www.kamagramama.com/]kamagra[/url]

OMaf

29th, Nov, 20

[url=https://www.tadalafilrembo.com/]cialis online[/url] [url=https://www.viagrage.com/]generic viagra[/url] [url=https://www.proscarhair.com/]finasteride[/url]

Tietty

30th, Nov, 20

Healthcare continues to be an outstanding, but divisive public topic. Not surprisingly, 41 percent of suitable voters said healthcare was their key issue in the mid-term elections in 2018.india viagra pharmacy In 2008, when the ACA became law, merely 46 percent of voters supported unique payer healthcare. That army has grown significantly to 59 percent put one’s imprimatur on in break of dawn 2018.[url=http://my-canadianpharmacyonline.com]viagra without a doctor prescription vip[/url] While Medicare-for-all legislation is unfit to pass both the Bordello and Senate in its … la mode manifestation, there is a switch manage in community notion with a valid the greater part in the present circumstances in favor.

Tietty

30th, Nov, 20

Healthcare continues to be an notable, but divisive bureaucratic topic. Not surprisingly, 41 percent of single voters said healthcare was their explanation issue in the mid-term elections in 2018.cialis generic best price In 2008, when the ACA became law, only 46 percent of voters supported single payer healthcare. That multitude has grown significantly to 59 percent put one’s imprimatur on in break of dawn 2018.[url=http://my-canadianpharmacyonline.com]canadian pharmacy meds[/url] While Medicare-for-all legislation is unthinkable to pass both the Line and Senate in its progress create, there is a paddle one’s own canoe in obvious conviction with a dynamic more than half at this very moment in favor.

YMaf

03rd, Dec, 20

[url=https://www.cialisay.com/]cialis online[/url] [url=https://www.viagrage.com/]viagra[/url] [url=https://www.kamagramama.com/]kamagra oral jelly 100mg[/url]

Leave a Comment

To preserve code added to a comment you can wrap your code in short tags
by using [square brackets]:

  1. PHP use - [php] <?php code here ?> [/php]
  2. CSS use - [css] #code-here {} [/css]
  3. HTML use - [html] <div> code here </div> [/html]
  4. JS use - [js] $(".codeHere") [/js]