WordPress.org

Ready to get started?Download WordPress

Forums

Two Different Excerpt Length's (8 posts)

  1. Calais97
    Member
    Posted 2 years ago #

    I write my own excerpts which I use on my home page with the usual read more links.
    I also have a featured section up the top with a larger excerpt area. My problem is I cant set the word limit on my own excerpts, I can with the auto generated excerpts but not my own.
    I want to show the full excerpt in the featured area and a shorter version of the excerpt in the read more section.

    Any ideas?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try this function: http://www.transformationpowertools.com/wordpress/automatically-shorten-manual-excerpt

    Or if you have many different excerpt sizes or want to show an exerpt outside the loop use functions like these: http://pastebin.com/aUZuQrZy
    put them in your functions.php

    And call the functions like this in your theme's template file:

    // inside the loop
    
    my_excerpt(); // just a regular WordPress excerpt (55 words)
    my_excerpt(30); // 30 words with formatting (<p>this is an excerpt ... [...]</p>)
    get_my_excerpt(30); // 30 words without formatting (this is an excerpt ... [...])
    
    // outside the loop
    // pass a Post ID to the function (required outside the loop)
    
    my_excerpt(30, 22); // 30 word excerpt of Post with ID 22
    get_my_excerpt(30, 22); // 30 word excerpt of Post with ID 22

    The functions both have two argument you can use

    my_excerpt($excerpt_length, $Post_id);

    These functions affect also the manual excerpt. That's why they strip all html tags from the excerpt content. Otherwhise it might break your site

  3. Calais97
    Member
    Posted 2 years ago #

    Your 2nd code sets the word number for the featured section but shows the full excerpt of the same post in the read more lower excerpt section.

    Maybe it would be easier to only have the featured post in the featured section and not anywhere else on the page? Is there a code for this?

  4. Calais97
    Member
    Posted 2 years ago #

    Or is there a code I can use that will set the word count of the manual excerpt only for the featured position?

  5. triggame
    Member
    Posted 2 years ago #

    Fantastic. Thanks Keesiemeijer!

  6. NormanBird
    Member
    Posted 2 years ago #

    Much thanks Keesiemeijer!!
    Just what I needed.

  7. LordAzriel
    Member
    Posted 2 years ago #

    If you use PHP > 5.3 you can make use of anonymous functions.

    In your functions.php:

    function custom_excerpt($new_length = 20, $new_more = '...') {
      add_filter('excerpt_length', function () use ($new_length) {
        return $new_length;
      }, 999);
      add_filter('excerpt_more', function () use ($new_more) {
        return $new_more;
      });
      $output = get_the_excerpt();
      $output = apply_filters('wptexturize', $output);
      $output = apply_filters('convert_chars', $output);
      $output = '<p>' . $output . '</p>';
      echo $output;
    }

    In your template:
    <?php custom_excerpt(40, 'moo') ?>

  8. raskull
    Member
    Posted 2 years ago #

    Awesome, LordAzriel! But could I trouble you illustrate a PHP<5.3 version of the above?

Topic Closed

This topic has been closed to new replies.

About this Topic