Conditional ACF fields within post layout
-
Hi,
I’ve setup a custom layout that needs to pull through ACF fields conditionally, so if they are blank the section won’t appear. I’m struggling to apply the condition, any help would be appreciated!
function my_custom_post_layout($layout, $post_id, $filter_id, $increment_post, $arrOptions) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'medium');
$wifi = get_field('wifi');
$parking = get_field('car_park_spaces');
$disabled = get_field('disabled_parking');
$rooms = get_field('other_meeting_rooms');
$capacity = get_field('seated_capacity');
$layout = '<div class="halldirectoryblock imageblock" style="background-image:url('.esc_url($featured_img_url).'); height: 100%;background-size:cover;background-position: center;"><a href="'.get_the_permalink($post_id).'" style="height:100%;"></a></div>';
$layout .= '<div class="halldirectoryblock" style="padding: 2em;vertical-align:top;">
<a href="'.get_the_permalink($post_id).'"><h3>'.get_the_title($post_id).'</h3></a>
<p class="capacity1">Capacity: '.$capacity.'</p>
<?php if($wifi) { ?><img src="/wp-content/uploads/2025/03/Wifi.svg" class="featureicon"/>
<?php } if($parking) { ?>
<img src="/wp-content/uploads/2025/03/Car-Parking.svg" class="featureicon"/><?php } if($disabled) { ?>
<img src="/wp-content/uploads/2025/03/Disabled-Parking.svg" class="featureicon"/><?php } if($rooms) { ?>
<img src="/wp-content/uploads/2025/03/Other-Meeting-Rooms.svg" class="featureicon"/><?php } ?>
<a href="'.get_the_permalink($post_id).'" style="float:right;"><img src="/wp-content/uploads/2025/03/Arrow.svg" style="width:24px;height:16px;" alt="Read More"></a>
</div>';
// $layout .= '<a class="'.esc_attr($arrOptions['class_popup']).'" data-postid="'.esc_attr($post_id).'" href="#">Open Popup</a>';
return $layout;
}
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
The topic ‘Conditional ACF fields within post layout’ is closed to new replies.