Conditional Output based on Manual Excerpt in Bricks

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.

Get access to all 526 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Pro
Conditional Section when Post has a Featured Image in Bricks

Conditional Section when Post has a Featured Image in Bricks

This Pro tutorial provides the steps to output a Section in Bricks on single posts (of any post type) only if the post has a…
Categories:
Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Checking if Repeater/Cloneable Meta Fields Are Empty in Bricks

Creating a condition to ensure that a section only gets output if at least one row of data is filled in.
Categories:
Pro
User Role Condition in Bricks

User Role Condition in Bricks

This Pro tutorial provides the steps to conditionally output elements depending on the currently logged-in user's role. Step 1 Create a section Template having the…
Categories:
Pro
Conditional Related Posts in Bricks

Conditional Related Posts in Bricks

How to output a section only if there is at least 1 related post for the current post being viewed.
Categories:
Checking if the current post has been published within the last x days

Checking if the current post has been published within the last x days

Looking to conditionally output an element if the current post (either in a query loop or when viewing a single post) has been published in…
Categories:
Conditionally Outputting Elements Only for Posts That Have a Specific Taxonomy Term

Conditionally Outputting Elements Only for Posts That Have a Specific Taxonomy Term

Using the core WordPress has_term() function for checking if the current post has a specific term of a given taxonomy.
Categories:
Pro
At Least 1 Search Result Condition in Bricks

At Least 1 Search Result Condition in Bricks

Registering a custom condition to render elements depending on whether there is at least one search result.
Categories:
JetEngine Checkbox Condition in Bricks

JetEngine Checkbox Condition in Bricks

How elements in Bricks can be conditionally output depending on the selected option from a JetEngine checkbox field.
Categories:
Tags: