Support » Fixing WordPress » Two Different Excerpt Length's

  • 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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    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

    Thread Starter Calais97

    (@calais97)

    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?

    Thread Starter Calais97

    (@calais97)

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

    Fantastic. Thanks Keesiemeijer!

    Much thanks Keesiemeijer!!
    Just what I needed.

    LordAzriel

    (@lordazriel)

    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') ?>

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Two Different Excerpt Length's’ is closed to new replies.