Get Your WordPress Excerpt by Word, not Character

As we know, wordpress give us a default excerpt content not by word. It always by character. Actually, it’s not a big problem for platform user. The web still has good running and it’s not influence to SEO position. Everything is fine.

But, for several visitors, excerpting content by character is a little disturbing. The latest word of excerpt usually not complete. There are two or more letters gone. This is its main lack. For example, your word content is ashamed and your excerpt just showing ash.. (read).

To solve this matter, many themes wordpress developer add several codes to their function’s theme file. How about us that have no function code? Don’t worry cause I will show you  simple tutorial code to show wordpress excerpt by word. It will be very easy.

First, open our theme’s function file. We can do it via web cpanel or wordpress dashboar menu (Appereance – Theme Editor – Function)

Second, add this below code on that function file.

<?php 
function nice_excerpt($numberword) { 
$texta = apply_filters('the_content', get_the_content()); 
$textb = strip_tags($texta); 
$textc = explode(" ",$textb); 
$resulttext = implode(" ", array_splice($textc, 0, $numberword)); 
echo ''.$resulttext.' ...(read more)'; 
?>

If we want to add our web name as intro, this is the full code.

<?php 
function nice_excerpt($numberword) { 
$texta = apply_filters('the_content', get_the_content()); 
$textb = strip_tags($texta); 
$textc = explode(" ",$textb); 
$resulttext = implode(" ", array_splice($textc, 0, $numberword)); 
$urla = esc_url( home_url( '' ) ); 
$urlb = strtoupper($urla); 
$webname = substr(strstr($urlb, '//'), strlen('//')); 
echo ''.$webname.' - '.$resulttext.' ... (read more)'; } ?>

Third, and this the final, add below code to everywhere that we want to excerpt. Just change the number to get our words.

<?php nice_excerpt(20); ?>

It so easy, isn’t it? Hope this simple tutorial about wordpress excerpt by word useful for all of us. Let me know your wordpress result excerpt. Have a nice day !!!

Leave a Comment