Hi @jarvis!
Sorry for opening this topic again but I have a little problem with the code which you kindly provided me with.
Using
<?php $image = get_field('image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
indeed shows no placeholder frame when there is no image — but if there is an image it will be shown twice.
Is there a way to avoid that?
Thank you very much in advance!
Hi @jarvis!
Thanks a lot for your answer — and your patience!
When I want to hide a specific div (class would be .hide-me) if “test” is empty — do have to use the same workaround as you described above by using `<style>
.hide-me {
display: none;
}
</style>`
Thank you!
Hi again @jarvis!
Please excuse but there is another question regarding the hide-code:
How do I have to change your code
<?php $image = get_field('image');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
when the custom field contains text instead of an image?
Thank you very much in advance!
Hi @jarvis!
I finally found a way to implement php code in my template — and your code works very well!
Thanks a lot!
Hi @jarvis — I can’t thank you enough!
I will try it and let you know if I was successful.
Hi @jarvis —
Thanks again for helping me!
Sorry — but what do you mean with template? Should I put the code in my “wpb-single-post.php” in my child theme folder?
Thank you!
Hi @jarvis and @teegee!
Thank you so much for trying to help me!
Unfortunately none of the code was working. The code from @jarvis was hiding the placeholder — But was also hiding it when I uploaded an image to the specific custom field. Also the code broke my template when I put it in my functions.php …
I don’t know … Maybe it’s a hopeless case …
Hi @jarvis!
Thank you very much — and sorry for not really understanding it …
What I’d like to achieve is to tell a div which has a certain class that it should not be displayed if a certain field is empty. I assume your code does this trick — but I don’t see where I have to put the class name of the div which should be hidden …
Thanks a lot!
Hi @jarvis!
Thanks a ton!
One (hopefully last) question: what should I write when I want to have that certain field (class maybe is “image_field”) empty instead of heaving a placeholder? Something like $display = "none";
?
Thank you!
Hi @jarvis
thanks a lot for your answer!
Following the manual on this page: https://www.advancedcustomfields.com/resources/hiding-empty-fields/
The code I am supposed to use is something like this:
`<?php if( get_field(‘field_name’) ): ?>
<?php endif; ?>
Sorry for being stupid — but how can I combine it with the your code?
Thank you!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.