Excerpt length in category archive and search pages

  • sylvia

    #3062

    Hi, can someone help me with the php code needed to limit the number of characters in excerpt view before the “continue reading” button is displayed?

    Basically, when someone clicks on category archives or performs a search, and the resulting excerpts are displayed…I want the continue reading button to be displayed after 150 characters. How?

    THANKS!

    Anonymous

    #23168

    You can use this function in your child theme functions.php file:

    function custom_excerpt_length($length) {
    return 150; // Change this to whatever you want.
    add_filter('excerpt_length', 'custom_excerpt_length');
    }

    Alternatively, you can use this plugin for even more control:

    http://wordpress.org/extend/plugins/advanced-excerpt/

    soapy

    #23169

    Hey Josh, as always, thanks for your reply. I pasted your code into my child theme functions.php….but it didn’t do anything. Here is my file:

    <?php
    function searchfilter($query) {

    if ($query->is_search) {
    $query->set('post_type',array('post'));
    }

    return $query;
    }
    add_filter('pre_get_posts','searchfilter');

    // Change some strings
    function graphene_filter_readmore( $translated, $original, $domain ) {
    $strings = array(
    'Continue reading &raquo;' => 'CONTINUE READING!!',
    'Read the rest of this entry &raquo;' => 'CONTINUE READING!!',
    );
    if ( ! empty( $strings[$original] ) ) {
    $translations = &get_translations_for_domain( $domain );
    $translated = $translations->translate( $strings[$original] );
    }
    return $translated;
    }
    add_filter( 'gettext', 'graphene_filter_readmore', 10, 3 );

    function custom_excerpt_length($length) {
    return 40; // Change this to whatever you want.
    add_filter('excerpt_length', 'custom_excerpt_length');
    }

    ?>

    Did I do something wrong? I changed the value to 40…but when I click on my category archives….its still the same as before….it still goes with the default length?

    Any thoughts?

    Admin

    Syahir Hakim

    #23170

    There’s a mistake in the code. Instead of this:

    function custom_excerpt_length($length) {
    return 40; // Change this to whatever you want.
    add_filter('excerpt_length', 'custom_excerpt_length');
    }

    it should be this:

    function custom_excerpt_length($length) {
    return 40; // Change this to whatever you want.
    }
    add_filter('excerpt_length', 'custom_excerpt_length');

    Anonymous

    #23171

    Oops. Thanks Syahir.

    soapy

    #23172

    Thanks guys…just a quick question, “return 40″…is that 40 characters or 40 words?

    Admin

    Syahir Hakim

    #23173

    40 words.

    soapy

    #23174

    Thank you!

    soapy

    #23175

    This still does not work. I pasted your code Syahir in my child theme functions.php file….but still no change. It still adds the continue reading button automatically. I changed return to 20 and expected to see each excerpt 20 words long and then the continue reading button…but no change….any ideas?

    Admin

    Syahir Hakim

    #23176

    Try inserting this instead:

    function your_custom_excerpt_length(){
    graphene_set_excerpt_length( 20 );
    }
    add_action( 'after_setup_theme', 'your_custom_excerpt_length' );
Viewing 10 posts - 1 through 10 (of 16 total)
  • You must be logged in to reply to this topic.
Do NOT follow this link or you will be banned from the site!