WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to change excerpt legth (3 posts)

  1. jepser
    Member
    Posted 3 years ago #

    Hi there,
    I'm trying to code a function (in functions.php) that returns me the number of characters in the excerpt, I think this example will illustrates better the scenario.

    In functions.php

    function custom_excerpt($excerpt_num){
    	$default_excerpt = get_the_content();
    	$new_default_excerpt = explode(' ', $default_excerpt, $excerpt_num);
    	array_pop($new_default_excerpt);
    	$new_excerpt = implode(' ', $new_default_excerpt);
    	return $new_excerpt;
    }

    In the template (inside a loop):
    <?php custom_excerpt(15) ?>
    Here I wanted to show an excerpt with 15 characters, but it's not returning nothig.

    Any solutions? thanks!

  2. chinmoy
    Member
    Posted 3 years ago #

    function custom_excerpt($excerpt_num){
    	$default_excerpt = get_the_content();
    	/*$new_default_excerpt = explode(' ', $default_excerpt, $excerpt_num);
    	array_pop($new_default_excerpt);
    	$new_excerpt = implode(' ', $new_default_excerpt);*/
            $new_excerpt = substr($default_excerpt, 0, $excerpt_num);
    	return $new_excerpt;
    }

    Try this.

  3. jepser
    Member
    Posted 3 years ago #

    thanks budy!
    I also changed return to echo...

Topic Closed

This topic has been closed to new replies.

About this Topic