In the Bricks Facebook group a user asks:
Is it possible to only show a post’s excerpt only if it has been manually set? In my blog post template, I want to display the excerpt above the main content if the author explicitly provided one. I tried using conditions on the Excerpt element but am not sure how to set it up properly.
Yes. Follow these steps.
Step 1
Define a custom function that takes in an optional post ID and returns true if the post has a manual excerpt or false if there is none.
Add the following code (thanks to AI) in child theme‘s functions.php
(w/o the opening PHP tag) or a code snippets plugin:
<?php
function bl_has_manual_excerpt( $post_id = null ) {
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
// Check if the post has an excerpt
if ( ! empty( $post->post_excerpt ) ) {
// Get the automatically generated excerpt
$auto_excerpt = wp_trim_words( $post->post_content, 55, '' );
// Compare the stored excerpt with the auto-generated one
return $post->post_excerpt !== $auto_excerpt;
}
return false;
}
Step 2
Whitelist the bl_has_manual_excerpt
function.
Ex.:
<?php
add_filter( 'bricks/code/echo_function_names', function() {
return [
'bl_has_manual_excerpt'
];
} );
You should also add other functions (native or custom) being used in your Bricks instance besides bl_has_manual_excerpt
. This can be checked at Bricks → Settings → Custom code by clicking the Code review button.
More info on whitelisting can be found here.
Step 3
Apply a dynamic data condition on an element in the single post template that you wish to be conditionally output depending on whether that post has a manual excerpt.
{echo:bl_has_manual_excerpt}
Check the result on the front end.