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..

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
Conditional Rendering in Bricks Based on CSS Class

Conditional Rendering in Bricks Based on CSS Class

Updated on 19 Jun 2024 A user asks: Is there a way to apply the same conditions to several elements like we set styles appending…
Categories:
Pro
Showing a Popup conditionally in Bricks based on Query Parameter

Showing a Popup conditionally in Bricks based on Query Parameter

Here's two ways a Bricks Popup can be shown on page load depending on URL parameter value.
Categories:
Tags:
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
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
Conditionally outputting an element in a Query Loop based on date-type custom field

Conditionally outputting an element in a Query Loop based on date-type custom field

This Pro tutorial shows how to output an element in a Bricks' query loop only if the value of a date-type of custom field for…
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
Conditional Rendering Based On Meta Box Group’s Sub Field

Conditional Rendering Based On Meta Box Group’s Sub Field

This Pro tutorial shows how we can output an element on a single post pages only if a specified sub-field (custom field created using Meta…