-
erwinmedia
I’m creating an Element template for my Blog Single Post. Now I want to output a custom ACF field (image) with dynamic data tags. Is this possible? If so, how do I do that?
Return Format: Image Array
REST API: Enabled
Field Type: ImageNow I have this dynamic tag “{{term_meta key:logo|tax:opdrachtgever}}” and the Return Format to Image URL.
Also good to know, it is related to a category section of a custom post type.
-
Hi there,
The element is for single posts, but you are trying to pull term meta, which won’t work, as the single post does NOT have term meta.
The term meta can only be pulled from a term archive.
-
erwinmedia
Hi Ying, thanks for your response.
I’m not quite sure what to do next. Just to clarify, this DOES work: “{{term_meta id:3|key:logo|tax:opdrachtgever}}”. But in this case, I’m passing a specific ID. How can I make this dynamic? “Tax:opdrachtgever” is linked to the relevant custom post type, which I’m creating this template for.
I realize I don’t fully understand it, so I’m trying to explain it as clearly as possible. If anything is unclear, please let me know.
-
Alvind
Hi there,
May I know where you assigned the ACF Image field? Is it attached to Posts or to a Taxonomy (like Categories or Tags)?
-
erwinmedia
Hi, to a Taxonomy
-
But in this case, I’m passing a specific ID. How can I make this dynamic?
You can do so for archive pages, but not single posts.
As I explained before, you can not pull term meta from a single post, and a post can have multiple categories.
So no, it’s not possible to pull the term image for the post that is assigned to that term. It will require custom development.
Just to confirm, do you only assign one category per post?
-
erwinmedia
I may have misunderstood some aspects earlier, and I appreciate your clarification. It wasn’t entirely clear to me what was or wasn’t possible, and I was simply responding to a question posed by your colleague.
To confirm: yes, each post is assigned to only one category.
-
Try this:
1. add class
my-category-image
to the image block in your single post content template.
Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/2. add this PHP code:
function render_term_image( $block_content, $block, $instance ) { if ( !is_admin() && !empty($block['attrs']['className']) && strpos($block['attrs']['className'], 'my-category-image') !== false ) { // Get the first term of taxonomy 'category' $terms = get_the_terms(get_the_ID(), 'category'); if ($terms && !is_wp_error($terms)) { $first_term =$terms[0]; // Get first term $term_id = $first_term->term_id; // Get term ID // Get the ACF image field value for this term $acf_term_image = get_field('logo', 'category_' . $term_id); if ($acf_term_image) { $term_image_output = '<img src="' . esc_url($acf_term_image). '">'; // Insert the image before the block content $block_content = $term_image_output; } } } return $block_content; } add_filter('render_block', 'render_term_image', 10, 3);
Adding PHP: https://docs.generatepress.com/article/adding-php/
- You must be logged in to reply to this topic.