• Resolved ThorHammer

    (@thorhammer)


    Hi there. I need to limit the length of characters in my excerpts on my front page.
    I am echoing them like this: <?php echo $post->post_excerpt; ?>
    In my other queries I am limiting the title output this way:

    <?php if (strlen($post->post_title) > 20) {
    echo substr(the_title($before = '', $after = '', FALSE), 0, 20) . '...'; } else {
    			the_title();
    			} ?>

    But it won’t work with my excerpts?

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter ThorHammer

    (@thorhammer)

    As far as I have found out, this is not possible to do with excerpts. I guess the soultion is to add a custom field and use this as an extra excerpts-container, cause this output is possible to manipulate. Please, correct me if I am wrong?

    Michael

    (@alchymyth)

    hi ThorHammer,

    no reason why this would not work:

    <?php if (strlen($post->post_excerpt) > 20) {
    echo substr($post->post_excerpt, 0, 20) . '...'; } else {
    echo $post->post_excerpt;
    } ?>

    or you could try this little string manipulation snippet, which will shorten the excerpt to a given number of characters and remove any broken last word as well: (adjust the number to your needs)

    <?php $text = $post->post_excerpt;
    if (strlen($text) > 150) {
    $text = substr($text,0,strpos($text,' ',150)); } ;
    echo apply_filters('the_excerpt',$text) . ' ...'; ?>

    btw: i think $post->post_excerpt will only show the excerpts that are written by hand into the excerpt field below the editor.

    Thread Starter ThorHammer

    (@thorhammer)

    alchymyth: Yes, it is the excerpt written into the field below the editor I am echoing on my front (index) page.
    And thank you again, your code actually solved my issue!!

    Thread Starter ThorHammer

    (@thorhammer)

    btw: How to escape the brutal line break before the “…” ?

    Michael

    (@alchymyth)

    i think by adding the … before applying the filter:
    (imho, apply_filters is wrapping the $text into paragraph tags, and when the dots are already in the $text, that should be ok)

    try:

    <?php $text = $post->post_excerpt;
    if (strlen($text) > 150) {
    $text = substr($text,0,strpos($text,' ',150)); } ;
    $text = $text . ' ...';
    echo apply_filters('the_excerpt',$text); ?>

    Thread Starter ThorHammer

    (@thorhammer)

    Life must be wonderful when you have an answere to everything. Thank you so much alchymyth!! You were right, again!

    Michael

    (@alchymyth)

    Life must be wonderful when you have an answere to everything.

    i would assume so …

    be assured that most of us only answer to the minute fraction of questions where we think we might have an anwer, and marvel at the majority of answers by other people.

    we all (that includes you) have the same amount of wisdom – just in different areas.

    Why do you still get the … even if you don’t fill up all the characters allowed?

    Awesome, but how about if I don’t want to cut off words? Explode the excerpt into an array. I’ll post my code after I get it working.

    Is there a way to rewrite alchymyth’s solution to work with the auto excerpt (or, even better – limit the auto excerpt by characters (rather than words) at the source)?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Limit excerpt length’ is closed to new replies.