22nd Jul '24
/
0 comments

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 610 Bricks code tutorials with BricksLabs Pro

Leave the first comment

 

Related Tutorials..

Checking for Post Type in Bricks

Checking for Post Type in Bricks

A way to output elements conditionally based on the current post type in the loop in Bricks.
Categories:
Pro
City-based and Country-based Conditional Output in Bricks

City-based and Country-based Conditional Output in Bricks

This Pro tutorial provides the steps to render a Bricks element only if the visitor is from the specified country or city. Note: Caching may…
Categories:
Render an element having a specific HTML ID based on value of a custom field

Render an element having a specific HTML ID based on value of a custom field

This tutorial provides the steps to use Bricks' bricks/element/render filter to conditionally output an element that has the specified HTML ID based on the value…
Categories:
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:
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:
Create A Customizable AJAX Add To Cart Button In Bricks Builder

Create A Customizable AJAX Add To Cart Button In Bricks Builder

In Bricks, you can simply create an Add To Cart button from a dynamic data / function {woo_add_to_cart}. This button supports AJAX as well if…
Pro
Condition for checking Bricks content

Condition for checking Bricks content

Only outputting Bricks content when a Page is built with Bricks, otherwise display WP content.
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: