WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to have two different excerpt lengths? (15 posts)

  1. seanjacob
    Member
    Posted 3 years ago #

    I have two loops on one page - popular and recent. I know how to change the excerpt length but don't know how to assign to a loop.

    Thanks.

  2. seanjacob
    Member
    Posted 3 years ago #

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

    This is by characters as well.

  3. seanjacob
    Member
    Posted 3 years ago #

    or you can make a function -

    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;
    }

    This also ends with a full word.

  4. seanjacob
    Member
    Posted 3 years ago #

    Update (above code wrong)

    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;
    }
  5. grayayer
    Member
    Posted 2 years ago #

    I'm attempting to use <?php echo substr(get_the_excerpt(), 0,30); ?> At the website PaintAA.com so that the excerpts on the right-hand side in the sidebar are much smaller than the excerpts shown on the actual blog page. But the problem is that the excerpts is based off of characters, rather than full words. I also have to add in additional code in order to get a "read more" link.

    So I'm curious about your function, get_the_popular_excerpt.
    How does one call that within a theme file?

  6. seanjacob
    Member
    Posted 2 years ago #

    All explained in this post (near the bottom) -

    http://wordpress.org/support/topic/limit-excerpt-length-by-characters

    If you still get stuck let me know.

  7. deepbevel
    Member
    Posted 2 years ago #

    You guys may be interested in this, should be really handy for what you're doing.

  8. grayayer
    Member
    Posted 2 years ago #

    thank you Sean, I did try using your function of "get_excerpt". And it works great apart from the fact that if a post includes a featured image which includes a caption, it also spits out that data in the paragraph. For example, it displays:

    [caption id="attachment_1843" align="alignright" width="300" caption="Several exterior colors can be used on Victorian homes."][/caption] Exterior paint color selection can be daunting for most of our clients. Unless... more

    funny thing is, it only does this for the first excerpt in a list. your other function, listed here as get_the_popular_excerpt worked well apart from missing an actual Permalink. but I got the hang of it and now include in my functions.php file this code:

    function get_the_frontpage_excerpt(){
    	$permalink = get_permalink($post->ID);
    	$excerpt = get_the_content();
    	$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    	$excerpt = strip_shortcodes($excerpt);
    	$excerpt = strip_tags($excerpt);
    	$excerpt = substr($excerpt, 0, 170);
    	$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    	$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    	$excerpt = $excerpt.'... <a href="'.$permalink.'">Continue reading →</a>';
    	return $excerpt;
    }

    And my front page template includes this code:
    <p><?php echo get_the_frontpage_excerpt(); ?></p>

    any way that you think that you can write the function so that it automatically wraps the excerpts in a paragraph like using <?php the_excerpt(); ?> does

  9. deepbevel
    Member
    Posted 2 years ago #

    The plugin I suggested lets you control 100% of the excerpt output, length by word, charachter, or sentance, and all mark up.
    Has global admin settings for all excerpts as well as a template tag that accepts query arguments so individual instances of the excerpt can be different.
    Makes this all really easy.

  10. seanjacob
    Member
    Posted 2 years ago #

    @deepbevel I find it better to learn how to do something (especially for something simple) than jumping straight to a plugin.

    @grayayer In the other post it already explains how to put it into a paragraph.

    Replace -
    $excerpt = $excerpt.'... <a href="'.$permalink.'">Continue reading →</a>';

    With -
    $excerpt = '<p>'.$excerpt.'... <a href="'.$permalink.'">Continue reading →</a></p>';

  11. grayayer
    Member
    Posted 2 years ago #

    @seanjacob
    That works perfectly, I'm sorry I didn't pick up on that in your other post. Thank you so much for all of your assistance with this. This really adds to my percieved value as a WordPress developer, much more so than if I was just dependent on plug-ins, which I do try to avoid as well.

    Do you have any recommendations for how I can go about learning the same knowledge about PHP structure that has allowed you to write your own custom functions here? I would love to be able to contribute back to the WordPress community and assist other novices.

    @deepbevel
    I do appreciate your recommendation to that plug-in. It's definitely good to know another option to do this, and an easier way to recommend for others unwilling to build into writing the code themselves.

  12. deepbevel
    Member
    Posted 2 years ago #

    Understood. When I have the time I'm the same way. However a good plugin can be a god send in a pinch. Carry on..

  13. seanjacob
    Member
    Posted 2 years ago #

    @grayayer Just keep doing what you done for this when you get stuck, google and learn. If you can't find what you are looking for post on the forums, WP has a awesome community.

    I like looking at tutorials todo new things with WP, http://wp.tutsplus.com/tag/basix/ is a good site.

  14. deepbevel
    Member
    Posted 2 years ago #

    Yes, me too. Greyayer, this forum is 100% responcible for everything I learned. I come everyday to help newbies and at the same time learn from experts.
    I don't do custom functions yet but I learned how to work with template code and custom queries, even css! I knew "0" about any of this only a year ago.
    It's not always easy to get someone as knowledgable and willing to spend the time as seanjacob, but they do show up if you're patient.

  15. Henry
    Member
    Posted 2 years ago #

    Having 2 excerpts works perfectly with the post formats that i've set up. I can now have a very brief excerpt on "image" and "gallery" posts yet a more descriptive excerpt on "standard" posts.

    Thanks for the function.

Topic Closed

This topic has been closed to new replies.

About this Topic