Home › Forums › Front-end Issues › Show ACF only if they exists in a div
Hi, I’m new with programming and I need support to show ACF field only in some articles templates with a layout like this : https://lucagrippo.it/vini/malbec-2019-trapiche-vineyards/
I want to stamp a div with $name (only if the field is not empty) and $value (only if the field is not empty).
I try this code found in documentation and works well, but I need different images for each field..and I don’t want to stamp $name if is empty. Hope anyone can help me thanks
<?php
$fields = get_fields();
if( $fields ): ?>
<div>
<?php foreach( $fields as $name => $value ): ?>
<h3><?php echo $name; ?></h3>
<img src="/icone/divisore.png" />
<p><?php echo $value; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php
$fields = get_fields();
if ($fields) {
foreach ($fields as $field => $value) {
if ($value) {
?>
<div>
<h3><?php echo $name; ?></h3>
<?php
switch ($name) {
case 'case':
$src = 'url for this field name';
break;
case 'for':
$src = 'url for this field name';
break;
case 'every':
$src = 'url for this field name';
break;
case 'possible':
$src = 'url for this field name';
break;
case 'field':
$src = 'url for this field name';
break;
case 'name':
$src = 'url for this field name';
break;
default:
// default for all field names not listed above
$src = 'default url';
} // end switch
?>
<img src="<?php echo $src; ?>" />
<p><?php echo $value; ?></p>
</div>
<?php
} // end if $value
} // end foreach field
} // end if fields
?>
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.