WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to change excerpt legth

[Resolved] How to change excerpt legth

  • 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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

    thanks budy!
    I also changed return to echo…

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] How to change excerpt legth’ is closed to new replies.
Skip to toolbar