• Resolved tb21

    (@tb21)


    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)
  • Plugin Author YMC

    (@wssoffice21)

    Hi!

    You will need to specify the ACF field, pass the post ID there, for example:

    $wifi = get_field('wifi', $post_id);
    Thread Starter tb21

    (@tb21)

    Hi!

    I’ve changed this, but I still get the following in my output:

    <!--?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 } ?-->
    Plugin Author YMC

    (@wssoffice21)

    Study the documentation on ACF fields and the plugin in more detail. Everything should work correctly here if you use the hook (filter) add_filter('ymc_post_custom_layout_545_1', 'my_custom_post_layout', 10, 5);. See the documentation on the plugin https://github.com/YMC-22/smart-filter

    https://prnt.sc/5F-HnZyPdp0f

    Thread Starter tb21

    (@tb21)

    Thanks, I managed to code a workaround by excluding the if fields from the layout.

    One other issue I am having is the “And” condition on the Taxonomy relation only works across different taxonomies. When I select multiple options from within the same taxonomy, it performs as if it is an “Or” condition.

    Plugin Author YMC

    (@wssoffice21)

    Relation
    How to select posts from specified taxonomies. Can be:
    AND – posts that are both included in the specified taxonomy terms.
    OR – posts that belong to any of the specified taxonomy terms.
    So this only applies if the post belongs to multiple taxonomies. And if the post belongs to only one taxonomies, this does not affect the selection of posts in any way. Read the documentation on using the global WP_Query object

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Conditional ACF fields within post layout’ is closed to new replies.