0

I have this code in my functions.php

function custom_excerpt_length() {

return 15;

}

add_filter('excerpt_length', 'custom_excerpt_length');

but it doesn't work as it gives me the full text and not the 15 words I specified. And the grid I have set up on my website is not right because of this.

my movie grid

Thanks in advance

1
  • Add third parameter as priority correctly and you have not pass parameter as on functions then please check your output. Nov 15, 2016 at 7:10

4 Answers 4

1

Also use this code for multiple type of getting excerpt

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'...';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
      $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
      return $excerpt;
    }

    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'...';
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }

then in your template code you just use..

<?php echo excerpt(25); ?>
0

Add third parameter and also you have not mention function parameter it will be worked

function custom_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
2
  • what is the third parameter for ? Nov 15, 2016 at 7:16
  • It's priority correctly for your function priority. And you have forgot to add function parameter like $length Nov 15, 2016 at 7:17
0

I've faced this problem too, your code is right. To solve this just go to Wordpress dashboard > posts > all posts then click edit in the post that has the problem, in the editor make sure that the excerpt is empty then click update. Click here to see the image

1
0

If you are using the "Excerpt" block in Gutenberg editor, and you are trying to set a default length for it (or anything other than the originally allowed 10-100 word value) you may find that the excerpt_length filter has no effect on the block, and you can't even use the_excerpt filter to override the excerpt's content. This is is because of the latest updates in the Excerpt block in WordPress.

However, you can use the following code to edit the default block settings of the Excerpt Gutenberg block:

function filter_metadata_registration( $metadata ) {
    if ($metadata['name'] === 'core/post-excerpt') {
        $metadata['attributes']['excerptLength'] = [
            'type' => 'number',
            'default' => 9999,
        ];
    }
    return $metadata;
};
add_filter( 'block_type_metadata', 'filter_metadata_registration' );

More on this: https://core.trac.wordpress.org/ticket/59348

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.