WordPress.org

Ready to get started?Download WordPress

Forums

How to make an excerpt longer? (6 posts)

  1. kev
    Member
    Posted 5 years ago #

    How can you make an excerpt longer than the standard length?

    Thanks.

  2. Len
    Member
    Posted 5 years ago #

    I believe the file you need to edit is located at wp-includes/formatting.php

    Look for excerpt_length The default is 55. However, rather than altering core files (which isn't a good idea) you may want to use a plugin. Perhaps there is something in this list you can use,

    http://wordpress.org/extend/plugins/search.php?q=excerpt

    Also, you do know that you can include your own "optional excerpts" right? Under the write post window there is a box for that purpose.

  3. kev
    Member
    Posted 5 years ago #

    thats great, however I would an image displayed within the excerpt.

  4. Len
    Member
    Posted 5 years ago #

    Actually another way to create custom length excerpts is to insert this in your theme's function.php file...

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');
    
    function custom_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = x;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '...');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }

    Adjust the x in $excerpt_length = x; to whatever you want.

    With respect to images if you're using excerpts on the front page you could use Custom Fields and place this in index.php right after the loop,

    <img src="<?php echo get_option('home'); ?>/wp-content/themes/YOUR-THEME/images/YOUR-IMAGE.png" width="50px" height="50px" alt="" />

    If you do this you'll have to play around with the style sheet to get it to look right.

    If you're using excerpts elsewhere images won't work. WordPress strips out all formatting on non-index.php/home.php post pages.

  5. nex2nun
    Member
    Posted 5 years ago #

    Here's a nice resource on using Custom Fields for creating article images.

    Web Designer Wall: WordPress Theme Hacks

    Thanks,
    Derek M.

  6. antistandard
    Member
    Posted 5 years ago #

    Thanks LenK for your solution. It is the best one I have read on the net and I have read a billion of them.

    If anyone needs to change the length of the excerpt and also change the [...] to a link to the full article then do what LenK suggested in your themes functions.php page:

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_trim_excerpt');
    function custom_trim_excerpt($text) { // Fakes an excerpt if needed
    global $post;
    if ( '' == $text ) {
    $text = get_the_content('');
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = x;
    $words = explode(' ', $text, $excerpt_length + 1);
    if (count($words) > $excerpt_length) {
    array_pop($words);
    array_push($words, '...');
    $text = implode(' ', $words);
    }
    }
    return $text;
    }
    ***** where x = excerpt word length******

    and

    change the line:
    array_push($words, '...');

    to:
    array_push($words, 'ID) . '">' . '... [ Read Full Article ]' . '');

    the only other thing you need to do is open your themes index.php page and change <?php the_content(); ?> to:
    <?php the_excerpt(); ?>

    hope this helps

    james

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.