tom84
tom84

Reputation: 401

hide ACF fields when they are empty

i use the following code for a wordpress template with custom fields:

<?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; ?>

With the code if (!get_field('special_product') ) i tryed to hide the custom field when it is empty but it doesnt work. How can i hide it?

Best regards

Chris

supplement: The Output for &special_product (when custom fields are empty) is:

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

Upvotes: 0

Views: 2095

Answers (1)

Daniel Vickers
Daniel Vickers

Reputation: 1084

When running print_r($special_product) you got:

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

Because the empty field returns an empty array and NOT nothing at all, it can never be deemed empty, you need to check a key from the array to see if the keys value is empty.

I told it to check the image field of that array to see if it's empty.

If your product wont always have an image then feel free to change it to check for a field that will always be present.

Here is your working code:

// 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; ?>

Upvotes: 2

Related Questions