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 626 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:
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
ACF Repeater sub field value dynamic data condition in Bricks

ACF Repeater sub field value dynamic data condition in Bricks

This Pro tutorial provides the steps to output an element on single posts (can be of any post type) only if a specified ACF (Pro)…
Weekday Condition in Bricks

Weekday Condition in Bricks

As of Bricks 1.7, the built-in "Weekday" condition does not work correctly. This is most likely due to it not taking the site's timezone (set…
Categories:
Is WooCommerce Cart Empty Condition in Bricks

Is WooCommerce Cart Empty Condition in Bricks

Updated on 5 Nov 2024 Looking to conditionally render an element depending on whether the user's cart is empty or not when using WooCommerce? Add…
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…
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:
Conditionally Outputting Based on Query Count in Bricks

Conditionally Outputting Based on Query Count in Bricks

Update on 22 Sep 2023: There's a much simpler built-in method now. See this post. In the Bricks Facebook group a user asks: Is there…
Categories:
[Function] Current Term Has Children

[Function] Current Term Has Children

Updated on 20 Jul 2024 A user asks: I'm working on an archive template for WooCommerce products. I'm trying to hide a section if there…