WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Excerpt length doesn't work (12 posts)

  1. batschi
    Member
    Posted 3 years ago #

    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?

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    function my_excerpt_length($length) {
    	return 20; // Or whatever you want the length to be.
    }
    add_filter('excerpt_length', 'my_excerpt_length');
  3. batschi
    Member
    Posted 3 years ago #

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

  4. esmi
    Forum Moderator
    Posted 3 years ago #

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

  5. batschi
    Member
    Posted 3 years ago #

    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.

  6. esmi
    Forum Moderator
    Posted 3 years ago #

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

  7. batschi
    Member
    Posted 3 years ago #

    yep, the same problem with posts.

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

  8. esmi
    Forum Moderator
    Posted 3 years ago #

    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()?

  9. batschi
    Member
    Posted 3 years ago #

    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.

  10. esmi
    Forum Moderator
    Posted 3 years ago #

    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.

  11. batschi
    Member
    Posted 3 years ago #

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

  12. esmi
    Forum Moderator
    Posted 3 years ago #

    As I understand it, yes.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags