sage-directives
sage-directives copied to clipboard
Wordpress @image directive returns image ID instead of get_field function when using ACF
When I using @image
directive with ACF key, Blade renders it as wp_get_attachment_image(<image id here>, ...)
instead of wp_get_attachment_image(get_field('acf key here'), ...)
. It is problem because image ID by ACF is dynamic and after change it in dashboard, changes does not make effect until rendered views will not be deleted. It is persistent on application in production mode and very dynamic content.
Are you passing it as a string?
Of course - my code looks like:
<div class="homepage-header-image">
@image('header.section.photo' , 'full', [])
</div>
and rendered blade looks like
<div class="homepage-header-image">
<?php echo wp_get_attachment_image(
22,
'full',
false,
[]
); ?>
</div>
So if I change image in Wordpress this change does not make effect until I don't clear rendered views.
I find solution by returning call to get_field
function instead of returning assigned image ID for moment of render.
I create own directive which is fair enough for this moment - code for it looks like duplicate of @image
but changing put of first index if is ACF field.
$expression = Util::parse($expression);
$image = Util::strip($expression->get(0));
if (
is_string($image) &&
!is_numeric($image) &&
Util::field($image)
) {
$fieldKey = $expression->get(0);
$expression = $expression->put(0, "get_field({$fieldKey})");
}
If we implement this on Wordpress directive this can be violation of SRP because WordPress do not have to worry about ACF. What do you think?
yes, unfortunately i have the same issue.
workaround:
@image(get_field('acf_image'))
Hello - was there ever a permanent solution to this issue? It appears to be similar to:
https://discourse.roots.io/t/blade-directives-for-sage/14301/32
And I'm having the same issue, though the solution above from @kukac7 doesn't work for me, for some reason