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

Leave the first comment

 

Related Tutorials..

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:
Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

Conditionally Hide a Section on a Specific Taxonomy’s Term Archives in Bricks

A user asked: I have a CTA in the footer of my site, which I have custom fields on various templates, etc to populate. I…
Categories:
Bricks Condition for WishList Member

Bricks Condition for WishList Member

How to render a Bricks element only if the user has access to a specified WishList Member level.
Categories:
Pro
Day of Week Condition in Bricks

Day of Week Condition in Bricks

Displaying elements conditionally based on the current day of the week.
Categories:
Pro
Condition to Check if the Current Category Has At Least One Child

Condition to Check if the Current Category Has At Least One Child

Looking to render an element in the category archive Bricks template only if the category of the current category archive page is a parent? This…
Categories:
Pro
Conditional CSS in Bricks based on Post Meta

Conditional CSS in Bricks based on Post Meta

In the Bricks Facebook group a user asked: Hi, how do you normally control margin, padding and font-size using dynamic data? I'm looking for the…
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:
Pro
Conditional Output in Bricks based on if Content has Headings

Conditional Output in Bricks based on if Content has Headings

How to prevent the output of the table of contents if the content has no headings.
Categories:
Tags: