• Resolved Calais97

    (@calais97)


    The following code only changed the length of the automatically generated excerpts. How do I apply these rules to change the length of my manual excerpts?
    What do I need to do to the following code?
    Any help would be appreciated, I’ve been trying to nut this one out for a while now.

    function excerpt_length($length) {
    	if( is_home() && !in_category('featured') ) {
    		return 70;
    	} else {
    		return 40;
    	}
    }
    add_filter('excerpt_length', 'excerpt_length');
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi

    While in essence what you are asking for is reasonable, the whole point of manual excerpts is to give you complete control over the contents of the excerpt, including its length, and the ability to use HTML tags, images, etc. in manual excerpts, that are stripped out of regular excerpts. When you put a length filter on a manual excerpt you also have to strip out HTML tags including links, because you will likely split the excerpt in the middle of a tag.

    It is possible to filter manual excerpts, but doing so is like paddling upstream against the current, in terms of overriding the whole reason manual excerpts were created in the first place. The simplest way to filter length on manual excerpts is write them shorter.

    That said, if you really really need to filter manual excerpts you can do it. They are found in $post->post_excerpt

    you can achieve what you want with this method:
    ‘Automatically Shorten the Manual Excerpt’, however, this approach also removes the html tags/formatting from the manual excerpt.

    Thread Starter Calais97

    (@calais97)

    Thanks for the reply. I found some code that did the trick.

    <?php
    $len = 200; //Number of words to display in excerpt
    $newExcerpt = substr($post->post_excerpt, 0, $len); //truncate excerpt according to $len
    if(strlen($newExcerpt) < strlen($post->post_excerpt)) {
        $newExcerpt = $newExcerpt."[...]";
    }
    
    echo "<p>".$newExcerpt."</p>"; //finally display excerpt
    ?>

    What can I do to add
    <?php the_excerpt(); ?>
    to the code so if there isn’t a manual excerpt it will display the auto excerpt?

    example:

    <?php
    if( $post->post_excerpt ) : //checks for manual excerpt
    $len = 200; //Number of words to display in excerpt
    $newExcerpt = substr($post->post_excerpt, 0, $len); //truncate excerpt according to $len
    if(strlen($newExcerpt) < strlen($post->post_excerpt)) {
        $newExcerpt = $newExcerpt."[...]";
    }
    
    echo "<p>".$newExcerpt."</p>"; //finally display excerpt
    else :
    the_excerpt();
    endif;
    ?>

    Thread Starter Calais97

    (@calais97)

    Thanks, worked perfect

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘excerpt_length on manual excerpts’ is closed to new replies.