WordPress.org

Forums

[resolved] excerpt_length on manual excerpts (6 posts)

  1. Calais97
    Member
    Posted 1 year ago #

    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');
  2. stvwlf
    Member
    Posted 1 year ago #

    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

  3. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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.

  4. Calais97
    Member
    Posted 1 year ago #

    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?

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    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;
    ?>
  6. Calais97
    Member
    Posted 1 year ago #

    Thanks, worked perfect

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.