-
aesthetik
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_imageThis 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.
-
Alvind
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.
-
aesthetik
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
andfeatured_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.
-
aesthetik
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:
-
Alvind
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 thefeatured_image
post meta and a Headline block with thestory_video
shortcode beneath it. Assign the Dynamic Image block the classcustom-featured-image
and the Headline block the classcustom-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.
-
aesthetik
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. -
Alvind
Sure, I’ve updated the snippet above to met those conditions. Just replace the previous snippet.
-
aesthetik
Got it working! Thanks.
-
Alvind
You’re welcome!
- You must be logged in to reply to this topic.