Overview…
As you may be aware, I have only used the standard Astra theme for the BMCS rebuild. I wanted to change the excerpt length on blog post cards displaying on the archive pages but, without the Astra Pro Addon† I’m so used to, I’d forgotten this was not an available option. So, plan B, a little manual coding.
What I initially thought would be a straightforward 2-minute task actually turned into a systematic debugging exercise that revealed a few important WordPress and Astra-specific behaviours which I thought were worth sharing (as I’m sure I’m not the only person who has wanted to make the same small change to the excerpt).
The Goal…
Change the excerpt length for blog post cards using the Astra theme’s blog-layout-6, targeting it with a child theme functions.php snippet.
The Debugging…
I searched through the Astra theme files to find the code I needed to adapt, added it to my functions file and hey presto… it didn’t work.
The original code:
/**
* Blog Custom excerpt length.
*
* @since 4.6.0
* @param int $length Length.
* @return int
*/
function astra_custom_excerpt_length( $length ) {
$blog_layout = astra_get_blog_layout();
return 'blog-layout-4' === $blog_layout ? 20 : $length;
}
add_filter( 'excerpt_length', 'astra_custom_excerpt_length', 1 );
Step 1: The Initial (Broken) Snippet
This was my starting point which seemed logical enough:
function bmcs_custom_excerpt_length( $length ) {
$blog_layout = astra_get_blog_layout();
return 'blog-layout-6-grid' === $blog_layout ? 30 : $length;
}
A couple of schoolboy errors meant I had the following issues:
- Missing add_filter() call
The primary issue. The function was declared but never hooked into WordPress, meaning it would never run regardless of anything else. - Wrong layout string
The targeted layout was blog-layout-6, not blog-layout-6-grid.
A third issue worth mentioning: I was attempting to debug the $blog_layout value using console.log() — a JavaScript habit that doesn’t exist in PHP. In PHP, the equivalent options are error_log() (writes silently to your server’s debug log, invisible to site visitors) or var_dump() (outputs directly to the page). A small but easy mistake to make if you context-switch between languages regularly.
Step 2: Adding the Missing Filter & Debugging
Adding add_filter() and correcting the layout string still produced no change. The next step was to isolate where the problem actually was. There were two possibilities — either the filter wasn’t firing at all, or it was firing but the layout string wasn’t matching. By stripping everything back to a hard-coded return 5, any layout logic is removed from the equation entirely. If excerpts change to 5 words, the filter is working and the layout string is the culprit. If nothing changes, the filter isn’t firing at all — and you need to look elsewhere.
// Test 1: Force a fixed return value
add_filter( 'excerpt_length', function( $length ) {
return 5;
}, 20 );
Still no change, confirming the filter was not firing at all. Cache was cleared (browser and hosting), and the child theme was confirmed working via other active snippets.
Step 3: Investigating Astra’s Excerpt Function
A further search through the Astra theme source files revealed that excerpt output is handled by a custom function in inc/markups-extra.php:
function astra_the_excerpt() {
$excerpt_type = apply_filters( 'astra_excerpt_type',
astra_get_option( 'blog-post-content' ) );
if ( 'full-content' === $excerpt_type ) {
the_content();
} else {
the_excerpt();
add_filter( 'excerpt_more', '__return_false' ); // Bug: too late!
}
}
Finding this function confirmed that Astra was calling the_excerpt() — so excerpt_length should have worked. That ruled out Astra’s custom call as the problem and pointed the investigation back toward the content itself. If the hook was correct and the function was being called, why was the filter still being ignored?
Interestingly, Astra’s own code adds the excerpt_more filter (a default WordPress hook) after the_excerpt() has already run and output its content — meaning it has no effect on that render. Whether this is an oversight or simply legacy code that was never cleaned up is unclear, but it’s a good illustration of how filter timing in WordPress matters.
Step 4: The Real Culprit — Manual Excerpts
The critical discovery: all posts on the site use manually entered excerpts (text entered in the Excerpt field in the WordPress post editor). WordPress’s excerpt_length filter only applies to auto-generated excerpts — trimmed from the post body content. It has no effect on manually entered excerpts.
This is why excerpt_length appeared completely ignored — it was never relevant to the content being output.
The Solution: get_the_excerpt Filter…
The get_the_excerpt filter fires for most excerpts — whether auto-generated or manually entered. Combined with wp_trim_words(), this gives full control over excerpt length regardless of source:
//-------------------------------
// Change Excerpt length on Blog Post Cards
//-------------------------------
// Note: excerpt_length filter does NOT work for manually entered excerpts.
// get_the_excerpt is used instead as it applies to all excerpt types.
add_filter( 'get_the_excerpt', function( $excerpt ) {
$blog_layout = astra_get_blog_layout();
if ( 'blog-layout-6' === $blog_layout ) {
return wp_trim_words( $excerpt, 30, '...' );
}
return $excerpt;
});
So, that was the initial problem solved. There was, however, a knock-on effect and it is something to be wary of when hooking into default code behaviour. I was also displaying the excerpt on the posts header hero section – for these I wanted to show a longer/full excerpt. The snippet was updated to target the two elements specifically:
//-------------------------------
// Change Excerpt length on Blog Post Cards
// Note: theme calls the excerpt via inc/markups-extra.php; astra_the_excerpt so astra_custom_excerpt_length does not work
//-------------------------------
add_filter( 'get_the_excerpt', function( $excerpt ) {
if (is_single()) {
return wp_trim_words( $excerpt, 60, '...' );
} else {
$blog_layout = astra_get_blog_layout();
if ( 'blog-layout-6' === $blog_layout ) {
return wp_trim_words( $excerpt, 30, '...' );
}
}
return $excerpt;
});
Key Takeaways…
- Always include add_filter()
Declaring a function without hooking it does nothing.
- excerpt_length only affects auto-generated excerpts
If your posts use the manual Excerpt field, this filter is completely bypassed. - get_the_excerpt is the reliable hook
It fires for all excerpts regardless of source. - Comment your workarounds
Future-you will thank present-you for explaining whyget_the_excerptwas used instead of the more obviousexcerpt_length. - Use Astra Pro
The no code option is obviously to use the Astra Pro Addon. Worth noting this is only a suggestion if you plan on utilising more of the PRO features.
