Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

How to display an icon only if the post in the loop has an excerpt

  • Hi there, so I have the following issue:

    – I have an SVG icon which I have created using the ‘Headline’ GenerateBlock on my website. This icon is part of a ‘query loop’
    – I only want this icon to display if the current post within the query loop has an excerpt field filled in.

    Please can you tell me how this would be possible?

    Thanks,
    Keith

  • Hi there,

    you could try something like this:

    1. add this PHP Snippet to your site:

    
    function remove_block_if_no_excerpt( $block_content, $block ) {
        if ( 
            !is_admin() && 
            ! empty( $block['attrs']['className'] ) && 
            strpos( $block['attrs']['className'], 'has-excerpt' ) !== false 
          ) {
            global $post;
            if ( empty( $post->post_excerpt ) ) {
                $block_content = '';
            }
        }
    
        return $block_content;
    }
    add_filter( 'render_block', 'remove_block_if_no_excerpt', 10, 2 );
    

    2. Select the block you want to display IF there is an excerpt, and in block settings Advanced > Additional CSS Classes add: has-excerpt

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.