如果ACF字段为空,如何隐藏DIV容器

发布于 2025-01-22 12:00:45 字数 4737 浏览 6 评论 0原文

我有此代码可以在页面上显示自定义ACF字段。当子字段为空时,我想完全隐藏DIV容器(class = Price-container和ikon-container),因为它为另一个Div容器的列中占用了空间,该容器应该填写空间。

如何解决这个问题? 谢谢

<?php if ( have_rows( 'menuer_layout') ) : ?>
            
        
                        <div class="col-sm-12 col-md-12 col-lg-4" style="padding:0">
                                <div class="sticky-top">
                                    <div class="price-container">

                                                <div id="pricebox-content" class="slick-slide-pricebox">
                                                    <div class="pricebox-container">
                                                        <div class="pricebox-top">
                                                            <h4 class="pricebox-header">One</br>Two</h3>
                                                            <span class="price-subtitle">Here goes..</span>
                                                        </div>
                                                    
                        
                                                                    
                                                        <?php while ( have_rows( 'menuer_layout' ) ) : the_row(); ?>
                                                            <div class="menucard-container">
                                                                <h5 style="margin:0"><?php the_sub_field( 'menu_overskrift' ); ?></h5>
                                                                <span><?php the_sub_field( 'menu_tekst' ); ?></span>
                                                                <?php $menu_knap_url = get_sub_field( 'menu_knap_url' ); ?>                                                         
                                                                    <?php if ( $menu_knap_url ) { ?>
                                                                        <div class="pricebox-link-container" style="padding-top: 5px">
                                                                            <a class="knap-2" href="<?php echo $menu_knap_url['url']; ?>" target="<?php echo $menu_knap_url['target']; ?>"><?php the_sub_field( 'menu_knap_tekst' ); ?></a>
                                                                        </div>
                                                                    <?php } ?>
                                                            </div>
                                                        <?php endwhile; ?>
                                                                <?php else : ?>
                                                                    <?php // no rows found ?>
                                                    
                                                    
                                                                <?php endif; ?>
                                                        
                                                    </div>
                                                </div>

                            </div>
                            
                            
            
            

                            <?php $hotel_ikoner_images = get_field( 'hotel_ikoner', 'option' ); ?>
                                <?php if ( $hotel_ikoner_images ) :  ?>
                            
                            
                                    <div class="ikon-container">
                                        <ul class="ikon-liste">
                                    <?php foreach ( $hotel_ikoner_images as $hotel_ikoner_image ): ?>
                                        <li class="hotel-ikon-spec">
                                            <img class="hotel-ikon-small" src="<?php echo esc_url( $hotel_ikoner_image['sizes']['thumbnail'] ); ?>" alt="<?php echo esc_attr( $hotel_ikoner_image['alt'] ); ?>" /> <span class="hotel-ikon-text"><?php echo esc_html( $hotel_ikoner_image['caption'] ); ?></span>
                                    </li>
                                    <?php endforeach; ?>
                                        </ul>
                                    </div>
                                <?php endif; ?>
                        </div>
                    </div>
        </div>
    </div>
</section>

I have this code to display custom ACF fields on a page. When the subfields are empty, i would like to hide the div containers (class= price-container and ikon-container) completely, as it takes space in the column for another div container which is supposed to fill out the space instead.

How to solve this?
Thanks

<?php if ( have_rows( 'menuer_layout') ) : ?>
            
        
                        <div class="col-sm-12 col-md-12 col-lg-4" style="padding:0">
                                <div class="sticky-top">
                                    <div class="price-container">

                                                <div id="pricebox-content" class="slick-slide-pricebox">
                                                    <div class="pricebox-container">
                                                        <div class="pricebox-top">
                                                            <h4 class="pricebox-header">One</br>Two</h3>
                                                            <span class="price-subtitle">Here goes..</span>
                                                        </div>
                                                    
                        
                                                                    
                                                        <?php while ( have_rows( 'menuer_layout' ) ) : the_row(); ?>
                                                            <div class="menucard-container">
                                                                <h5 style="margin:0"><?php the_sub_field( 'menu_overskrift' ); ?></h5>
                                                                <span><?php the_sub_field( 'menu_tekst' ); ?></span>
                                                                <?php $menu_knap_url = get_sub_field( 'menu_knap_url' ); ?>                                                         
                                                                    <?php if ( $menu_knap_url ) { ?>
                                                                        <div class="pricebox-link-container" style="padding-top: 5px">
                                                                            <a class="knap-2" href="<?php echo $menu_knap_url['url']; ?>" target="<?php echo $menu_knap_url['target']; ?>"><?php the_sub_field( 'menu_knap_tekst' ); ?></a>
                                                                        </div>
                                                                    <?php } ?>
                                                            </div>
                                                        <?php endwhile; ?>
                                                                <?php else : ?>
                                                                    <?php // no rows found ?>
                                                    
                                                    
                                                                <?php endif; ?>
                                                        
                                                    </div>
                                                </div>

                            </div>
                            
                            
            
            

                            <?php $hotel_ikoner_images = get_field( 'hotel_ikoner', 'option' ); ?>
                                <?php if ( $hotel_ikoner_images ) :  ?>
                            
                            
                                    <div class="ikon-container">
                                        <ul class="ikon-liste">
                                    <?php foreach ( $hotel_ikoner_images as $hotel_ikoner_image ): ?>
                                        <li class="hotel-ikon-spec">
                                            <img class="hotel-ikon-small" src="<?php echo esc_url( $hotel_ikoner_image['sizes']['thumbnail'] ); ?>" alt="<?php echo esc_attr( $hotel_ikoner_image['alt'] ); ?>" /> <span class="hotel-ikon-text"><?php echo esc_html( $hotel_ikoner_image['caption'] ); ?></span>
                                    </li>
                                    <?php endforeach; ?>
                                        </ul>
                                    </div>
                                <?php endif; ?>
                        </div>
                    </div>
        </div>
    </div>
</section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

春风十里 2025-01-29 12:00:45

您可以使用get_sub_field

    <?php if(!get_sub_field('field_name')){ ?>

        <div>
         Hide This Content If Field Empty
        </div>

    <?php } ?>

You Can Use get_sub_field
https://www.advancedcustomfields.com/resources/get_sub_field/

    <?php if(!get_sub_field('field_name')){ ?>

        <div>
         Hide This Content If Field Empty
        </div>

    <?php } ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文