Custom Excerpts Per Page

I’m trying to set up a specific page to use a specific number of characters in an excerpt. I tried using this code, but it broke the site:

function wpse61271_custom_excerpt_length( $length ) 
{
    if ( 
       is_front_page()
       XOR is_home()
    )
       return 50;

    // return default length
    return $length;
}
add_filter( 'excerpt_length', 'wpse61271_custom_excerpt_length', 999 );

Is there an alternate bit of code that could work better?

Read More

EDIT: Here’s the full loop, in case that helps:

<?php while ( have_posts() ) : the_post(); ?>
                        <a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                        <div class="storyVideo">
                            <p><?php the_post_video(); ?></p>
                        </div><!--/.storyVideo-->
                        <div class="storyExcerpt">
                            <?php
                                function wpse61271_custom_excerpt_length( $length ) 
                                {
                                    if ( 
                                       is_front_page()
                                       XOR is_home()
                                    )
                                       return 50;

                                    // return default length
                                    return $length;
                                }
                                add_filter( 'excerpt_length', 'wpse61271_custom_excerpt_length', 999 );
                            ?>
                            <p><a class="button" href="<?php the_permalink(); ?>">Read More</a></p>
                        </div><!--/.storyExcerpt-->
                        <div style="clear:both;"></div>
                    <?php endwhile; ?>
                    <?php if(function_exists('wp_paginate')) {
                        wp_paginate();
                    } ?>

Related posts

1 comment

  1. Here’s a solution that should do what you want (per your questions in comments and chat):

    Functions.php

    function wpse102641_custom_excerpt_length( $length ) 
    {
        // assuming your category is called "Stories"
        if ( is_category(  'Stories' ) ) {
           return 50;
        }
    
        // return default length
        return $length;
    }
    add_filter( 'excerpt_length', 'wpse102641_custom_excerpt_length', 999 );
    

    References

    Codex:

Comments are closed.