WordPress.org

Ready to get started?Download WordPress

Forums

PHP Question... (8 posts)

  1. nickaster
    Member
    Posted 3 years ago #

    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!!!

  2. ClaytonJames
    Member
    Posted 3 years ago #

    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.

  3. DigitalSquid
    Member
    Posted 3 years ago #

    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;
  4. nickaster
    Member
    Posted 3 years ago #

    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!

  5. DigitalSquid
    Member
    Posted 3 years ago #

    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.

  6. Simon Prosser
    Theme Review Admin
    Posted 3 years ago #

    or simply add to functions.php

    function new_excerpt_length($length) {
    	return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length')
  7. nickaster
    Member
    Posted 3 years ago #

    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!

  8. Simon Prosser
    Theme Review Admin
    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.