How do you add a custom excerpt length in twenty eleven child theme?

I have a problem displaying custom excerpt length. I've searched for a solution on wordpress stackexchange and found this:

    remove_filter( 'excerpt_length', 'twentyeleven_excerpt_length' ); 
add_filter('excerpt_length', 'new_excerpt_length');
function new_excerpt_length($length) {
  return 50;
}

When adding this to functions.php I still get the same 3 line excerpt as before. If I change the return statement to something else like, return 8 or return 17, I still get the same excerpt as before.

The excerpt function is inserted into content.php like this:

div class="entry-content"
            ?php the_excerpt(); ?
            ?php wp_link_pages( array( 'before' = 'div class="page-link"span' . __( 'Pages:', 'twentyeleven' ) . '/span', 'after' = '/div' ) ); ?
        /div!-- .entry-content --
        ?php endif; ?

What am I doing wrong here?

Topic theme-twenty-eleven excerpt Wordpress

Category Web


In your second approach remove <?php the_excerpt(); ?> and replace it with:

<?php echo wp_trim_words( get_the_excerpt() , 60); ?>

it will show 60 words change to word amount you want.

Edit
I replaced exact following in content.php:

<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->

with this:

with:

<div class="entry-content">
<?php echo wp_trim_words( get_the_excerpt() , 60); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'twentyeleven' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->

Works like a charm if not than you have changed already some more somewhere else in the code?
I assume you made a child-theme of it or are you changing original code? When it is in the org theme..put your copy you made before changing anything back in and replace exact the code as shown here, you will see it works! If it is in your child theme..replace it also with a fresh copy of that file and replace exact code as shown. I can change it into any number..even with not adding excerpt in backend it shows exact that what it should.

Note: all done and checked in a "virgin" Twenty eleven theme. It works if you follow and do exact as shown and you didn't made any changes in the functions.php or somewhere else which overrules this. Good luck and have a nice weekend.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.