Calling a custom excerpt function in a local loop

I have a function that sets the excerpt length of the first post differently than the posts that follow, but I cannot get it to work. Any input would be appreciated.

functions.php (the function)

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

add_filter( 'excerpt_length', 'custom_excerpt' );`

home.php (the loop)

<div class="section-content section-news-content">
<?php

$args = array(
        '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();
            get_template_part( 'template-parts/content', 'news' );
        }
            wp_reset_postdata();
    }
?>
</div>`

content-news.php (calling the function from the loop)

<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

        custom_excerpt();

        ?>
    </span>
 </div><!-- .entry-content -->
     </div>

</div>
</article>`

EDIT: My question has not quite been answered and I have just realised why. To clarify the excerpts are not manually added they are the_excerpt of the_post ‘s. Also intrinsic to my question yet inexplicably left out is that I have multiple loops and each may need a different excerpt length, eg first loop, only first excerpt is different. Second loop, third excerpt is different. My apologies for the lack of clarification.

1 Answer
1

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