WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Excerpt length doesn't work

[Resolved] Excerpt length doesn't work

  • Hello guys,

    I have a little problem.
    After I have added

    add_action('init', 'my_custom_init');
    function my_custom_init() {
    	add_post_type_support( 'page', 'excerpt' );
    }

    to my functions.php, the

    function excerpt_length( $length ) {
    	return 20;
    }
    add_filter( 'excerpt_length', 'excerpt_length' );

    doesn’t work anymore.

    All pages have a excerpt and I want to display an excerpt of 20 words only. Could anyone help me with this problem?

Viewing 11 replies - 1 through 11 (of 11 total)
  • esmi

    @esmi

    Forum Moderator

    function my_excerpt_length($length) {
    	return 20; // Or whatever you want the length to be.
    }
    add_filter('excerpt_length', 'my_excerpt_length');

    this is what I added, but after I have added the first piece of code the filter excerpt_length doesn’t work anymore.

    esmi

    @esmi

    Forum Moderator

    Sorry – my bad. It might be worth raising this as a bug in Trac if you can replicate it in the Twenty Ten theme.

    I can replicate it in the Twenty Ten theme.
    So I think this could be a bug for the page excerpt if you use a excerpt and not the more-tag.

    esmi

    @esmi

    Forum Moderator

    It looks like the excerpt_length filter isn’t being applied correctly. Does it fail in Posts as well?

    yep, the same problem with posts.

    EDIT: but if I use the more-tag, it works correctly

    esmi

    @esmi

    Forum Moderator

    if I use the more-tag, it works correctly

    The more tag only works with the_content() – not the_excerpt(). Are you sure that the relevant template files are using the_excerpt()?

    yep – I have added the_excerpt() but I don’t mean the more-tag sorry.
    I meant if the excerpt field is empty in the backend for a page then I will get the correct length from the content. But I want to use the excerpt field.

    esmi

    @esmi

    Forum Moderator

    Ah! I’m not 100% sure that the excerpt_length filter will run on the Optional Except field – if that’s what you mean. I’d guess that the assumption was, if you manually added an excerpt, you’d want it all to be shown. The filter only runs on the auto-generated excerpt.

    ah okay – the filter will work with the automatically generated excerpt only?

    esmi

    @esmi

    Forum Moderator

    As I understand it, yes.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Resolved] Excerpt length doesn't work’ is closed to new replies.