Calling a custom excerpt function in a local loop

Note, the excerpt_length filter does not work like that. It takes one parameter, $length, which is the word length of the excerpt.

Updated solution

First we wire up the callback function wpse_default_excerpt_length() to the excerpt_length filter. This is our default excerpt length handler.

Next, some additional excerpt_lengthcallback functions are declared, but we won’t wire them up here. Instead, that will be handled within the custom queries themselves (see the home.php section further down).

functions.php

/**
 * Set the excerpt lengths for the **main** query.
 *
 * @param int $length Excerpt length.
 * @return int
 */
add_filter( 'excerpt_length', 'wpse_default_excerpt_length', 100 );
function wpse_default_excerpt_length( $length ) {
    global $wp_query;

    // If we're on the first post of the first page of posts:
    if ( $wp_query->current_post == 0 && ! $wp_query->is_paged ) {
        $length = 55;
    } else { // All other posts:
        $length = 20;
    }

    return $length;
}

/**
 * Set the excerpt lengths for a custom query - first post.
 */
function wpse_custom_loop_intro_excerpt_length( $length ) {
    return 60;
}

/**
 * Set the excerpt lengths for a custom query - other posts.
 */
function wpse_custom_loop_excerpt_length( $length ) {
    return 30;
}

home.php

<div class="section-content section-news-content">
<?php
    $args = [
        'posts_per_page' => 5,
        'post_type' => 'post',
        'cat' => 2,
    ];
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            // If we're on the first post of the first page of posts:
            if ( $query->current_post == 0 && ! $query->is_paged ) {
                // Add our particular excerpt_length filter. Note that at priority 200,
                // it will override wpse_default_excerpt_length() at priority 100
                add_filter( 'excerpt_length', 'wpse_custom_loop_intro_excerpt_length', 200 );

                get_template_part( 'template-parts/content', 'news' );

                // Clean up after ourselves.
                remove_filter( 'excerpt_length', 'wpse_custom_loop_intro_excerpt_length', 200 );        

            } else { // All other posts in this custom loop:
                add_filter( 'excerpt_length', 'wpse_custom_loop_excerpt_length', 200 );

                get_template_part( 'template-parts/content', 'news' );

                remove_filter( 'excerpt_length', 'wpse_custom_loop_excerpt_length', 200 );  
            }
        }
        wp_reset_postdata();
    }
?>
</div>

content-news.php

Controlling the excerpt length is done via the excerpt_length filters added previously. Use the_excerpt() to output the excerpt.

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="excerpts">
        <div class="post-text">
            <div class="entry-content">
                <span class="entry-text"><?php the_excerpt(); ?></span>
            </div><!-- .entry-content -->
        </div>
    </div>
</article>

Leave a Comment