• I’m using this code to make my post excerpts shorter:

    <?php function new_excerpt_length($length) {
    	return 13;
    }
    add_filter('excerpt_length', 'new_excerpt_length'); ?>
    <?php remove_filter('the_excerpt', 'wpautop'); ?>

    But there are 2 sections on my blog that require different excerpt lengths. Is it possible to specify 2 excerpt lengths so that I can specify in one of my loops to use the excerpt length from one function and another excerpt length for the other section of my blog?

    PS: There are 3 loops on my homepage, but I’m only worried about 2.

Viewing 8 replies - 1 through 8 (of 8 total)
  • function new_excerpt_length($length) {
    	global $post;
    	if ($post->post_type == 'post')
    		return 13;
    	else if ($post->post_type == 'post_2')
    		return 26;
    	else if ($post->post_type == 'post_3')
    		return 39;
    	else
    		return 55;
    }
    add_filter('excerpt_length', 'new_excerpt_length');

    and you might want to add a priority argument to your add_filter statement, just to be safe, as shown here: https://codex.www.ads-software.com/Plugin_API/Filter_Reference/excerpt_length

    This is what I need but I am not understanding the post type ==’post_2′ coding.

    I have looked at the codex page but still not fully understanding.

    https://codex.www.ads-software.com/Post_Types

    A custom post type just to get a different excerpt length? I wouldn’t do it that way. Just apply truncation right at the point in your template where you need it. The following will output the excerpt, truncated to the nearest whole word under the length you specify (75 characters in this example, but you can put whatever number you want in there):

    <?php echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0, 75), ' ' ) ); ?>

    Thank you for the reply, I also have been doing some research.

    This is simpler
    <?php echo substr(get_the_excerpt(), 0,30); ?>

    Could also make it a function which also ends with a full word.

    function get_the_other_excerpt(){
    $excerpt = get_the_content();
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $the_str = substr($excerpt, 0, 50);
    $the_str = trim(preg_replace( '/\s+/', ' ', $the_str));
    return $the_str;
    }

    Call
    <?php echo get_the_other_excerpt(); ?>

    The intent of my less simple approach is to truncate a string to the nearest whole word, rather than cutting off in the middle of a word. I explain my reasoning here:

    https://www.ambrosite.com/blog/truncate-long-titles-to-the-nearest-whole-word-using-php-strrpos

    Your example function is good, though. Stripping out the shortcodes and tags can be important in some situations.

    My apologies ambrosite I didn’t realise your code was doing that, I am still learning. Also nice blog post.

    Update to my code.

    function get_the_popular_excerpt(){
    $excerpt = get_the_content();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, 40);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
    return $excerpt;
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Possible to have 2 excerpt lengths?’ is closed to new replies.