1

Searched everywhere but have not seem this come up. I'm looking to set the length of the excerpts in the loop in functions.php but I need the first post only to have double the characters as the rest.

Example: 1st post set to 60 characters, following posts set to 30.

I tried this but it doesn't work:

function custom_excerpt_length( $length ) 
{
    static $instance = 0;
    return ( in_the_loop() && 0 == $instance++ ) ? 60 : 30;
}
add_filter( 'excerpt_length', 'custom_excerpt_length' );

Any ideas?

1 Answer 1

0

To determine where you are inside the loop in this filter, you will have to access the global main query. Like this:

add_filter ('excerpt_length', 'wpse268679_custom_excerpt_length');

function wpse268679_custom_excerpt_length ($length) {
  // access main query
  global $wp_query;
  // do this only for the main loop and the first post in the query
  if (is_main_query() && ($wp_query->current_post == 0))
    $length = 60;
  else
    $length = 30;
  return $length;
  }

The above will work for the main loop only. If you have a local loop, you cannot access the query globally, so you will have to build your own excerpt function, which passes the query in stead of the post. This isn't that difficult. Like this:

 wpse268679_custom_excerpt ($query) {
   if ($query->current_post == 0)
     $excerpt = wp_trim_words ($query->post->post_excerpt, 60);
   else
     $excerpt = wp_trim_words ($query->post->post_excerpt, 30);
   return $excerpt;
   }

Beware that above will need tweaking, for instance to take into account cases where there is no excerpt defined (in which case you may want to use the post content to trim).

14
  • Thanks for this really useful particularly about the global variable. The codex mentions only using the variables listed, would this approach be considered good practice. I'm just learning and don't want to pick up bad habits : ) Testing now will mark correct shortly.
    – JPB
    May 31, 2017 at 13:06
  • It does not seem to affect the first post? Only this variable is having an effect and it affects all the posts? $length = 30;
    – JPB
    May 31, 2017 at 13:12
  • I've tested this in my own install and it works. Possibly some plugin is messing with the excerpt. Are you sure the filter is called at all?
    – cjbj
    May 31, 2017 at 13:35
  • No plugins yet its a fresh _s theme. Got the excerpts showing correctly originally with this query: function small_excerpt_length( $length ) { return 20; } add_filter( 'excerpt_length', 'small_excerpt_length' ); so I removed it and added your code. There are multiple loops on the page but that shouldnt matter should it? It should affect the first post of each loop?
    – JPB
    May 31, 2017 at 13:39
  • So, the filter is called. Try echoing something inside the condition to see if it is applied correctly
    – cjbj
    May 31, 2017 at 13:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.