WordPress change default excerpt length

Excerpt is the easiest way to display trimmed down version of full description of a wordpress post. You can display excerpt by simply use the_excerpt(); function anywhere in your theme files. By default wordpress has set the excerpt length to 55 words. So, it is important for wordpress developers to know a way to change default excerpt length of wordpress post as in theme development we can’t limit to 55 words.

I’m going to share a function which you can change default excerpt length in this article.

Change Excerpt Length to a fixed value

You can simply change the default excerpt length value to another value. In this case, the excerpt length is a static value throughout the website, but the value is different to the default one. To do that you can simply add following code to the functions.php file in your wordpress theme.

function your_theme_excerpt_length($length) {
    return 200;
add_filter('excerpt_length', 'your_theme_excerpt_length');

And simply call to the_excerpt() function as you do it with default excerpt length.

But it is better to have a way to use different excerpt length values throughout the theme. ie. it is very useful if you can call to the excerpt by passing excerpt length as a parameter of the function.

Calling to the_excerpt function passing excerpt length as a function parameter

In the following function, excerpt length is taken as a parameter of the function. And replace default excerpt length with it. And also “Read More” link also altered according to my need. You can alter it to match your needs. Simply add following code to the functions.php file of your wordpress theme.

function custom_excerpt($excerpt_length = 55, $id = false, $echo = true) {
    $text = '';
    if($id) {
        $the_post = & get_post( $my_id = $id );
        $text = ($the_post->post_excerpt) ? $the_post->post_excerpt : $the_post->post_content;
    } else {
        global $post;
        $text = ($post->post_excerpt) ? $post->post_excerpt : get_the_content('');

    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);
    $text = strip_tags($text);

    $excerpt_more = ' ' . '<a href="'. get_permalink($id) . '">Read More...</a>';
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
        $text = implode(' ', $words);
        $text = $text . $excerpt_more;
    } else {
        $text = implode(' ', $words);
        echo apply_filters('the_content', $text);
        return $text;

and call to the function anywhere you want to display excerpt in your theme files.


Thats it! we are done! Try it and share your thoughts bellow.

How to show posts from a category?

Have you ever wanted to show wordpress posts from a category? Well, a customer had a such requirement recently and I’m going to share my code with some comments here. I know there are number of plugins to do the same, but we have more control when we write our own code in our theme.

In the first step lets write our code snippet in the functions.php file of our theme as a shortcode and use it in theme files. In the next step we will integrate it to a plugin.

Ok, its time to exercise. Open functions.php file of your active theme and copy and past following code in it.

function unc_show_posts_by_category($attr){
    /** wp query to get posts from specific category */
    $cat_name = $attr['category_name'];
    $num_posts = $attr['num_posts'];
    $unc_query = new WP_Query( array('category_name' => $cat_name, 'posts_per_page' => $num_posts));

    $results = '';
    /** starting the loop */
        $results .= '<ul class="muaw-posts-by-category">';
            $results .= '<li>';
            if(has_post_thumbnail()) {
                $results .= '<a href="' . get_the_permalink() . '" rel="bookmark">' . get_the_post_thumbnail(get_the_ID(), array(50, 50)) . get_the_title() . '</a>';
            } else {
                $results .= '<a href="' . get_the_permalink() . '" rel="bookmark">' .get_the_title() . '</a>';
            $results .= '</li>';
        $results .= '</ul>';

    return $results;

    /* Re-set original Post Data */

// Add a shortcode
add_shortcode('muaw_category_posts', 'unc_show_posts_by_category');

Now your theme is ready to use shortcode. This function queries to get specific number of posts from a certain category. If featured image is set, this code snippet will display the post thumbnail too. And at the end we defined a shortcode ‘muaw_category_posts’ to simplify it’s usage.

There are 3 different ways to display posts in a certain category using above code.

First method is to call to the function directly in a preferred place inside your theme. To do that simply past the following code in a preferred theme file ( such as page.php, sidebar.php etc… )

$attr['category_name'] = "uncategorized";
$attr['num_posts'] = 5;
echo unc_show_posts_by_category($attr);

Make sure to change ‘uncategorized’ to a desired category slug and 5 to the number of posts you need to display.

And second method is to call the shortcode inside a theme file. Same as above, past the following code in a desired place of a desired theme file to show posts from a certain category.

<?php echo do_shortcode( '[muaw_category_posts category_name="uncategorized" num_posts="5"]' ); ?>

Here also you will have to change the parameters to fit your needs.

The last method of applying this code to your theme is pretty easy. In the post/page editor, you can past the bellow code and that’s it.

[muaw_category_posts category_name="uncategorized" num_posts="5"]

In order to show the list of posts, you may do some css coding in your theme’s stylesheet. Happy coding!