Create your own Excerpt in WordPress

Sometimes we need to limit how many words will be displayed in our excerpt, If you have use WordPress 2.9.x you can use Easy Excerpt method, but if you want something different, you can create your own excerpt and replace your original excerpt.

Custom-Excerpt-in-WordPress-460x431

Custom Excerpt in WordPress

To create own excerpt in your WordPress, write down this code below in your functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php add_filter('the_excerpt''my_excerpts');
function my_excerpts($content = false) {
            global $post;
            $mycontent $post->post_excerpt;
            $mycontent $post->post_content;
            $mycontent = strip_shortcodes($mycontent);
            $mycontent str_replace(']]>'']]&gt;',$mycontent);
            $mycontent strip_tags($mycontent);
            $excerpt_length = 55;
            $words explode(' '$mycontent,$excerpt_length + 1);
            if(count($words) > $excerpt_length) :
                array_pop($words);
                array_push($words'...');
                $mycontent = implode(' '$words);
            endif;
            $mycontent '<p>' $mycontent '</p>';
// Make sure to return the content
    return $mycontent;
}
?>

 

For some reason, if you want to keep original excerpt and only need this code to display on special place, you can remove the add_filter functions and then place this code in whatever you want it. Please note this code must be placed in The Loop.

1
<?php echo my_excerpts(); ?>

You have done modified original excerpt with your custom excerpt in WordPress. Original Idea, come from Justin Tadlock