Archived topic
Dynamic Image Post Meta Not Working
11 replies · Started by William on January 23, 2022
Hi there,
Iam trying to update a theme to incorporate your dynamic image block (really cool update by the way). I currently have a shortcode that takes a custom field image from a category level and applies it to a post level:
add_shortcode( 'category_image', function() {
$categories= get_the_category();
$term = get_category($categories[0]->term_id);
$category_image = get_field( 'category_image', $term );
$category_image_html = '<img class="ba-archive-image" src="' . $category_image['url'] . '">';
return $category_image_html;
} );
This means at a post level, if the shortcode appears, the image appears.
However, sticking 'category_image' into the post meta in the dynamic image block does not seem to do anything.
I am wondering why this is the case? Is there a way that this can be achieved?
Kind regards,
Will
Hi Will,
If you change the line get_the_category(); to get_the_category( get_the_ID() );, does it display anything?
I'd suggest doing a var_dump of every variable to see which ones it not giving the intended value so you can be sure where the issue starts. :
The shortcode works perfectly well and your change the shortcode still works when I place [category_image] in a post. It just does not work with your dynamic image block for some reason, when choosing post meta and then putting in 'category_image'
The above code displays the category image for the category that the post is under for a post. So I guess it's strictly not post meta or is it? Is there some way to use it and link it to your dynamic image block?
Hi there,
you can try:
1. In ACF save the Image ID not the URL.
2. In Dyanamic Image Block - use the Term Meta and add the category_image field name.\
Unfortunately, that does not seem to work.
I appreciate this may not apply to the dynamic block - in short, is there a way to take an ACF field applied to the category of a post and have it dynamically added to posts inside that category?
Isn't that what your current shortcode is doing ?
Yes, it works perfectly - I'm wondering if the dynamic image and other dynamic blocks of Generate Press are post specific or if they go beyond post data to things such as tags and categories?
There are some limitations at this time, such as dealing with Custom Fields that are stored in an Array.
This is particularly an issue with Category Terms, as the posts can have many, which we addressed in the shortcode but isn't something that we can do in Dynamic Data ( yet ).
I should have spotted the above issue originally :) Shortcode is the way to go for now.
Thanks!
You're welcome
Sorry David but just to confirm. If there was an ACF field for a category and the 'Container' block was used on an 'Element' for a post, with the 'Dynamic Background Image' being chosen as 'Term meta' and then adding just the ACF field, which is an 'Image ID', or 'Image URL' or 'Image Array', for that matter, it currently is not possible to show that ACF field for the category for which the post is in?
It's possible but you'll need some sort of workaround.
Like David's mentioned, if it's an array then it would display because the dynamic background will get Array for the URL of the image.
The dynamic background is applied through a dynamic value for background-image property in CSS.
This property is expecting the exact path to the image so if the returned value of the custom field is actually an image but in an array, you'll need a helper function that will iterate from the array so we can get the actual exact path to the image file.
About the helper function:
The helper function's will have to print a CSS, applying a background image to the container block w/ the dynamic background.
It's quite tricky to do and it's actually not worth the trouble when you can just set the ACF's return format to Image URL.
...it currently is not possible to show that ACF field for the category for which the post is in?
To clarify: If you're trying to display the custom field value of a term on a single post page, it is possible.
But not within the dynamic block elements as you'll probably have to create a shortcode for this.
The shortcode's task is basically to get the post's taxonomy term and get the term's ACF value.
But this is quite tricky as well as there are considerations like:
- If a post has multiple terms, which term's ACF value should be taken?
> > If its for a single term only, what term should be picked? Is there any preferred "primary taxonomy"?
> > If it's for all the terms, how would you want it to be displayed?