【问题标题】:hide ACF fields when they are empty为空时隐藏 ACF 字段
【发布时间】:2019-04-11 13:39:03
【问题描述】:

我将以下代码用于带有自定义字段的 wordpress 模板:

<?php

// vars
$special_product = get_field('special_product', $term); 

if (!get_field('special_product') )
if ( $special_product): ?>
<div class="container_wrap container_wrap_first main_color sidebar_right 
template-shop">
  <div class="container">
    <div class="container-left-sp single-product-main-image alpha">

    <a class="lightbox-added lightbox avia_image" href="<?php echo $special_product['image']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
    <img src="<?php echo $special_product['image']['url']; ?>" alt="<?php echo $special_product['image']['alt']; ?>" /></a>

    <div class="thumbnails">

    <a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb1']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
    <img width="100" height="100" src="<?php echo $special_product['thumb1']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb1']['alt']; ?>" /></a>
           <a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb2']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
    <img width="100" height="100" src="<?php echo $special_product['thumb2']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb2']['alt']; ?>" /></a>
           <a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb3']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
    <img width="100" height="100" src="<?php echo $special_product['thumb3']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb3']['alt']; ?>" /></a>
    </div>

</div>
<div class="container-right-sp single-product-summary">
    <?php echo $special_product['description']; ?>
    <a class="avia-slideshow-button avia-button avia-color-orange avia-multi-slideshow-button" href="<?php echo $special_product['product-link']['url']; ?>">
    Zum Produkt</a>
    </div>    

        <div>
</div>
</div> <!--close container -->
</div>   <!--close container_wrap --> 

<?php endif; ?>

使用代码 if (!get_field('special_product') ) 我尝试在自定义字段为空时隐藏它,但它不起作用。怎么隐藏?

最好的问候

克里斯

补充: &special_product 的输出(当自定义字段为空时)是:

数组([图片] => [描述] => [产品链接] => [thumb1] => [thumb2] => [thumb3] => )

【问题讨论】:

    标签: wordpress advanced-custom-fields


    【解决方案1】:

    运行 print_r($special_product) 时,您得到:

    Array ( [image] => [description] => [product-link] => [thumb1] => [thumb2] => [thumb3] => )
    

    因为空字段返回的是一个空数组而不是什么都没有,它永远不能被认为是空的,你需要从数组中检查一个键来查看键值是否为空。

    我告诉它检查该数组的图像字段是否为空。

    如果您的产品不总是有图片,请随意更改它以检查始终存在的字段。

    这是你的工作代码:

    // vars
    $special_product = get_field('special_product', $term); 
    
    <?php if ( $special_product['image'] ): ?>
        <div class="container_wrap container_wrap_first main_color sidebar_right 
        template-shop">
            <div class="container">
                <div class="container-left-sp single-product-main-image alpha">
                    <a class="lightbox-added lightbox avia_image" href="<?php echo $special_product['image']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
                    <img src="<?php echo $special_product['image']['url']; ?>" alt="<?php echo $special_product['image']['alt']; ?>" /></a>
    
                    <div class="thumbnails">
                        <a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb1']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
                        <img width="100" height="100" src="<?php echo $special_product['thumb1']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb1']['alt']; ?>" /></a>
                        <a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb2']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
                        <img width="100" height="100" src="<?php echo $special_product['thumb2']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb2']['alt']; ?>" /></a>
                        <a class="lightbox-added lightbox avia_image thumb-image" href="<?php echo $special_product['thumb3']['url']; ?>"  rel="lightbox" title="" rel="product_images[grouped]">
                        <img width="100" height="100" src="<?php echo $special_product['thumb3']['url']; ?>" class="attachment-shop_thumbnail size-shop_thumbnail" alt="<?php echo $special_product['thumb3']['alt']; ?>" /></a>
                    </div>
                </div>
                <div class="container-right-sp single-product-summary">
                    <?php echo $special_product['description']; ?>
                    <a class="avia-slideshow-button avia-button avia-color-orange avia-multi-slideshow-button" href="<?php echo $special_product['product-link']['url']; ?>">
                Zum Produkt</a>
                </div>    
            </div> <!--close container -->
        </div>   <!--close container_wrap --> 
    <?php endif; ?>
    

    【讨论】:

    • 这就是它通过文档的工作方式,因此您的 get_field 一定不能正常工作,如果您使用 the_field,它是否会执行该值,因为您的 $term 可能无法正常工作
    • 嗯..好的!我不知道如何解决它。字段始终显示(我是否为空)
    • 自定义字段只有在我开始使用时才会显示:$special_product = get_field('special_product', $term); if (get_field('special_product', $term) ): ?&gt; &lt;div class="container_wrap container_wrap_first main_color sidebar_right....
    • 更新了答案,还有special_product的字段类型是什么
    • 是的,这是使用的代码...但是当自定义字段为空时,它也会显示所有 div 容器:-(
    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 2019-01-04
    • 2013-09-03
    • 1970-01-01
    • 2013-10-02
    • 2020-12-21
    • 1970-01-01
    • 2020-07-17
    相关资源
    最近更新 更多