1

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?

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();
                    } ?>
  • Changed the wording so I'm no longer asking for a plugin. I don't get why asking for a plugin is against the rules, it seems perfectly on topic to me. I'm trying to solve a WordPress problem, a plugin may solve it. – JacobTheDev Jun 11 '13 at 14:38
  • 1
    Plugin recommendations are explicitly off-topic per the FAQ. – Pat J Jun 11 '13 at 14:41
  • That's great, but they shouldn't be. – JacobTheDev Jun 11 '13 at 14:41
  • 2
    Can you show us the PHP error you get? (wonder why are you using XOR) – birgire Jun 11 '13 at 14:42
  • 2
    A plugin recommendation falls into the "please do my homework for me" category-- aka, "does not show any research effort". Why should I, or anyone else, search the plugin repository for you? Or spend time searching the web for something that probably isn't going to meet your precise requirements anyway? But back to the topic: How does this break the site? That is a pretty strict condition you have but I can't get anything to actually break. – s_ha_dum Jun 11 '13 at 14:45
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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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