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.

Using ACF tag, show oEmbed in place of featured image if value exists

  • Hello, trying to replicate (in a way) some functionality for featured images in my custom post template element. Similar to how if there’s no image available, then hide. But instead, I’d like to check if a post meta field (story_video) contains a value, display. If not, show the featured_image (both fields using ACF).

    My custom post type template has a featured image using using the GBP Image tag. I have set the dynamic data to be:

    Data Source: Post Type
    Post Type: Links
    Image Source: Post meta
    Post Meta Field: featured_image

    This works great.

    But what I would like to do is check if the Post Meta Field “story_video” contains data, and show that. If not, then it would default to the “featured_image” field. And then, if possible, if neither contain data, just hide the featured image. Right now I can get it to check if there is a video link, and show/remove, but not hide the image if video exists.

    Would I do this by setting up multiple containers, one for each (video and image), then handle via block settings (remove container condition)? If so, how do I control it so that if both have values, only the one appears? If I have a video, I still want featured image to appear so it will display thumbnails in archives.

    Can I even embed a video this way? I tried to use the GBP Headline block, and all it does is publish the text link of the youtube video instead of embed the video.

    I haven’t been able to get this working yet. So any help in direction appreciated.

  • Hi there,

    You might need to use a shortcode to return the embed link. Try adding this snippet:

    
    add_shortcode( 'story_video', function() {
       return get_field( 'story_video', get_the_ID());
    });
    

    Then, in the Headline block, remove the dynamic condition and just paste the shortcode as text: [story_video].

    Perhaps the multiple containers method is the way to go. If both custom fields have values, you can conditionally render only one using a render_block filter. Let me know if you need help with that.

    However, the way you added the featured image via the custom field—-the archive page will not detect it because WordPress looks for the default featured image set using the native option in the post.

  • Thank you for this! It works. I am able to embed the video.

    However, you will need to help me with the render_block filter. I am not familiar with that. So any help is appreciated.

    Also, do you know how I can make the video embed at 100% of my container? It only fills 3/4 of it.

  • I think using the generateblocks_dynamic_content_output would be a better option.

    1. Add a CSS class to the headline block, eg. my-video.

    2. Add this PHP code, make sure the story_video and featured_image are the correct field name.

    add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block) {
        if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'my-video' ) !== false ) {
            $story_video = get_field('story_video', get_the_ID());
            $featured_image = get_field('featured_image', get_the_ID());
    
            if ($story_video) {
                $content = $story_video;
            } elseif ($featured_image) {
                $content = $featured_image;
            }
        }
        return $content;
    }, 10, 3);

    3. You can remove the shortcode PHP code.

  • Thanks Ying. I have added the php code and tried what you said, but now nothing appears.

    https://www.temporary-url.com/B89E9

    Here is my setup in the builder:

  • The generateblocks_dynamic_content_output filter isn’t working because it requires the block to be dynamic, which it isn’t in this case.

    Here’s how I would approach it: keep the story_video shortcode snippet provided earlier, then inside the content template element, add both a Dynamic Image block to display the featured_image post meta and a Headline block with the story_video shortcode beneath it. Assign the Dynamic Image block the class custom-featured-image and the Headline block the class custom-story-video.

    When you view a post with both a featured image and story video, you’ll see both rendered on the frontend. To conditionally render them, add this snippet:

    add_filter('render_block', function ($block_content, $block) {
        if (is_admin() || empty($block['attrs']['className'])) {
            return $block_content;
        }
    
        // Get both meta values upfront
        $featured_image = get_post_meta(get_the_ID(), 'featured_image', true);
        $story_video = get_post_meta(get_the_ID(), 'story_video', true);
        
        // If both exist, only show blocks with featured_image class
        if ($featured_image && $story_video) {
            if (strpos($block['attrs']['className'], 'custom-story-video') !== false) {
                return '';
            }
        } else {
            // If either is missing, hide their respective blocks
            if (strpos($block['attrs']['className'], 'custom-featured-image') !== false && !$featured_image) {
                return '';
            }
            if (strpos($block['attrs']['className'], 'custom-story-video') !== false && !$story_video) {
                return '';
            }
        }
    
        return $block_content;
    }, 10, 2);
    

    With this, if a featured image exists but no story video, only the featured image will display, and vice versa. If both exist, only the featured image will render.

    Lastly, you can adjust the video dimensions within the oEmbed field settings in ACF.

  • Thank you, I feel like I am so close! I implemented what you said, its working, but one last item.

    I was trying to get the opposite result that you laid out in your code.

    1. If post has both featured image and video, only show video.
    2. If post has only video, show video.
    3. If post has only featured image, show featured image.
    4. If post does not have both, don’t show either.

  • Sure, I’ve updated the snippet above to met those conditions. Just replace the previous snippet.

  • Got it working! Thanks.

  • You’re welcome!

  • @Alvind, a follow up question on this as I start to expand on it.

    I’m trying to implement this on my regular posts and want to swap my default featured image with a video, rather than the custom field “story_video”. Can you tell me what I need to replace “story_video” with to show the default WP featured image? I have searched and tried:

    get_the_post_thumbnail($post->ID, 'post-single', true); and featured_image but that doesn’t seem to work.

    Do you know what the default db field name is for the featured image?

  • The featured image isn’t stored as a meta field called “featured_image”—it’s stored with a special meta key called “_thumbnail_id”, which contains the attachment (image) ID from the media library.

    So to check if a post has a featured image, you should use:
    has_post_thumbnail(get_the_ID())

    Instead of get_the_post_thumbnail($post->ID, 'post-single', true)

    Here’s the updated snippet:

    add_filter('render_block', function ($block_content, $block) {
        if (is_admin() || empty($block['attrs']['className'])) {
            return $block_content;
        }
        
        // Check if post has featured image and story video
        $has_featured_image = has_post_thumbnail(get_the_ID());
        $story_video = get_post_meta(get_the_ID(), 'story_video', true);
        
        // If both exist, only show blocks with featured_image class
        if ($has_featured_image && $story_video) {
            if (strpos($block['attrs']['className'], 'custom-story-video') !== false) {
                return '';
            }
        } else {
            // If either is missing, hide their respective blocks
            if (strpos($block['attrs']['className'], 'custom-featured-image') !== false && !$has_featured_image) {
                return '';
            }
            if (strpos($block['attrs']['className'], 'custom-story-video') !== false && !$story_video) {
                return '';
            }
        }
        
        return $block_content;
    }, 10, 2);

    The change you need to make is updating the Dynamic Image block type to Featured Image.

  • Yes, thank you! This worked.

    But one thing I am finding is that I am no longer able to hide the featured image when I check the box off in “disable elements”

  • Not sure about your full use case here, but the snippet should handle the visibility of the featured image. I don’t think you need to use the Disable Elements option.

  • In this use case there are some regular posts that don’t need a featured image. I do add a featured image so that it shows up in the query on archive pages or other query loops, but when visiting the page, its not necessarily required for that post.

    I don’t have a definitive rule structure for this. Some posts will need to display the featured image, and then some won’t. I’ve pinged you some examples in the Private Information box for your reference. I’ve check the box to disable the featured image on some posts, but they still appear. Wasn’t sure if this snippet was overriding that.

  • The option in disable elements only works for the default featured image, the PHP snippet is to alter a block’s output; it has nothing to do with the default featured image.

    You are using a GB image block in the post’s content template to display the featured image, so the option in the disale elements can never control it.

    You can remove the GB image block from the content template and enable the default featured image, then you can control its visibility with the disable elements option.

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