• Hey guys,

    Right now I’m displaying post excerpts on my Tag templates as follows:

    ‘<?php strip_tags( the_excerpt() ); ?>’

    Sometimes, however, the excerpt is too long and messes up the page layout. How can I force the exceprt to cut off after a certain number of words or characters? Like this:

    ‘<?php strip_tags( the_excerpt(CUT OFF AFTER 100 CHARS) ); ?>’

    THANKS!!!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Is it possible to just add a filter to lower the default excerpt length similar to something like this?

    Control Excerpt Length using Filters

    Or would that cause conflict in this case with excerpts used elsewhere on your site? Just for my own curiosity, does “strip_tags” have any effect on the_excerpt in this case? – or would html be automatically stripped anyhow? Just ball-parking, mind you. I guess I really don’t know for sure.

    DigitalSquid

    (@twelvefootsnowman)

    Maybe something like:

    $length = 30; //Or whatever number of words you want
    $excerpt = strip_tags(get_the_excerpt());
    $words = explode(' ', $excerpt);
    if(count($words) > $length ){
        array_splice($words, $count);
        $excerpt = implode(' ', $words);
    }
    echo $excerpt;
    Thread Starter nickaster

    (@nickaster)

    Thanks guys… I don’t understand where all that php would go… this is in the tag.php template. Can you lay it out within the example I have above?

    Thanks!

    DigitalSquid

    (@twelvefootsnowman)

    Yup, just replace <?php strip_tags( the_excerpt() ); ?> with:

    $length = 30; //Or whatever number of words you want
    $excerpt = strip_tags(get_the_excerpt());
    $words = explode(' ', $excerpt);
    if(count($words) > $length ){
        array_splice($words, $count);
        $excerpt = implode(' ', $words);
    }
    echo $excerpt;

    In your tag.php template.

    or simply add to functions.php

    function new_excerpt_length($length) {
    	return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length')

    Thread Starter nickaster

    (@nickaster)

    okay, super.. thanks… one more clarification. for functions.php, sure I can add that, but do i just slap in there exactly as you have it written? And then the code I have on tag.php doesn’t need to be changed?

    Thanks!

    The code I posted will change the excerpt length wherever get_the_excerpt() is called

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘PHP Question…’ is closed to new replies.