скрыть поля ACF, когда они пусты - PullRequest
0 голосов
/ 11 апреля 2019

Я использую следующий код для шаблона 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')) я попытался скрыть настраиваемое поле, когда оно пустое, но оно не работает. Как я могу это скрыть?

С наилучшими пожеланиями

Chris

дополнение: Выходные данные для & special_product (когда настраиваемые поля пусты):

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

1 Ответ

2 голосов
/ 11 апреля 2019

При запуске 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; ?>
...