Support

Account

Home Forums ACF PRO Show icon if checkbox is checked

Solved

Show icon if checkbox is checked

  • I’m using some checkboxes to display Holiday Home Facilities.

    field name is hh_facilities
    The single holiday home pages list all facilities using:

    <?php 
    $field = get_field_object('hh_facilities');
    $value = $field['value'];
    $choices = $field['choices'];
    
    if( $value ): ?>
    <ul>
    	<?php foreach( $value as $v ): ?>
    	<li>
    		<?php echo $choices[ $v ]; ?>
    	</li>
    	<?php endforeach; ?>
    </ul>
    <?php endif; ?>

    That works fine.

    On the archive page, I would like to display “Pet Friendly” if the pet friendly box is checked.

    I’ve been using this (taken from the conditional code here – https://www.advancedcustomfields.com/resources/checkbox/)

    <?php $pets = get_field('hh_facilities'); 
    if( $pets && in_array('pet', $pets) ): ?>Pet Friendly<?php endif; ?>

    Nothing happens.

    I have tried echo ing the output of $pets just to see if it’s working … again, nothing.

    I’m sure I’m missing something obvious but just can’t work it out – would be very grateful for any help

    cheers

    webecho

  • going to need some more information. Is the code on your archive page inside “The Loop”. You probably need to supply a bit more code.

  • Hi John
    Here’s the full page code, less a few irrelevant bits…

    <?php get_header(); ?>
    <section class="content">
    
    <?php 
    
    if (have_posts() ) {
    query_posts($query_string . '&orderby=title&order=ASC');
    	while (have_posts() ) {
    		the_post(); ?>
    		
    <?php
    
    $guests = get_field('guests');
    $bedrooms = get_field('bedrooms');
    $king = get_field('king');
    $queen = get_field('queen');
    $double = get_field('double');
    $single = get_field('single');
    $bunk = get_field('bunk');
    $trundle = get_field('trundle');
    $sofa = get_field('sofa_bed');
    $lowday = get_field('low_day');
    
    ?>		
    
    <article class="promo">
    <a href="<?php echo get_permalink(); ?>">
    <img src="<?php the_field('accom_feature_photo', $post->ID); ?>" width="100%" alt="<?php the_field('accom_name');?> - <?php single_cat_title(); ?>" >
    <?php if($lowday): ?><div class="price">from <span>$<?php echo $lowday; ?></span></div><?php endif; ?>
    <h2><?php the_field('accom_name');?></h2>
    </a>
    <section class="quick-intro">
    <?php if (has_term('dunsborough-holiday-homes', 'type')) : ?>
    <ul class="quick-info">
    <?php if ($guests): ?><li><i title="guests" class="fa fa-user" aria-hidden="true"></i> <span> <?php echo $guests; ?></span></li> <?php endif; ?>
    <?php if ($bedrooms): ?><li><i title="bedrooms" class="fa fa-bed" aria-hidden="true"></i> <span> <?php echo $bedrooms; ?></span></li> <?php endif; ?>
    
    <?php $pets = get_field('hh_facilities'); 
    if( $pets && in_array('pet', $pets) ): ?><i ti-friendly" class="fa fa-paw" aria-hidden="true"></i>Pet<?php endif; ?>
    
    </ul>
    <?php endif; ?>
    
    </section>
    
    </article>
    	
    	<?php
    		
    	} // end while
    } // end if
    ?>
    </section>
    <?php get_footer(); ?>

    Thanks for taking a look

  • Is hh_facilities a checkbox field or a true/false field?

  • Hi John
    hh_facilities is the Field Group containing all the checkboxes

  • Now you’ve completely lost me. Is hh_facilities like a clone field or something? I have no idea what this field is by looking at your code and your description of it as a “field group” makes it even less clear.

    Maybe you need to check what the this is actually returning

    
    <?php 
      $pets = get_field('hh_facilities');
      var_dump($pets);
    ?>
    
  • Hi John

    I have created a Field Group with the following inside:

    Label – Holiday Home Facilities
    name – hh_facilities
    type – checkbox
    choices –
    Air Conditioning
    BBQ facilities
    Beachfront
    Big Screen TV

    Pet Friendly

    Using the var_dump spits out the array:

    Array
    array(24) { [0]=> array(2) { ["value"]=> string(16) "Air Conditioning" ["label"]=> string(16) "Air Conditioning" }

    I am trying to check and see if the checkbox Pet Friendly is checked.
    If it is, echo Pet friendly.

  • The error message on screen is
    “Notice: Array to string conversion in …”

  • This reply has been marked as private.
  • This reply has been marked as private.
  • Can you give me a screenshot of the field? It looks like you’re returning the label instead of the value but you’re checking for the value.

  • Hi John
    Yes, I think that’s what’s happening.

    I have changed the code to use the value instead of the label, but it’s still showing on all of them?

    <?php $pets = get_field_object('hh_facilities'); 
    if( in_array('pet', $pets) ): ?><i title="Pet Friendly" class="fa fa-paw" aria-hidden="true"></i><?php endif; ?>
  • Could it be the return value part?

  • With return value, I’ve tried the 3 options:

    1. Value
    This works and display the label correctly ONLY if I have removed the VALUE ‘pet’ from pet: Pet Friendly in Choices.

    2. Label
    This works and display the label correctly ONLY if I have removed the VALUE ‘pet’ from pet: Pet Friendly in Choices.

    If i leave the ‘pet’ in i.e. pet: Pet Friendly
    I get Notice: Undefined index: Pet Friendly in

    3. Array (both)
    Error on every facility
    Warning: Illegal offset type in

    It appears I need to use the Array option so it can collect both the value and the label.

    If that’s the case, I’ll need to redo the code to display all the Facilities…
    which is currently:

    <?php 
    $field = get_field_object('hh_facilities');
    $value = $field['value'];
    $choices = $field['choices'];
    
    if( $value ): ?>
    <ul>
    	<?php foreach( $value as $v ): ?>
    	<li>
    		<?php echo $choices[ $v ]; ?>
    	</li>
    	<?php endforeach; ?>
    </ul>
    <?php endif; ?>
  • If you are returning the value then this should be correct

    
    <?php $pets = get_field_object('hh_facilities'); 
    if( in_array('pet', $pets) ): ?><i title="Pet Friendly" class="fa fa-paw" aria-hidden="true"></i><?php endif; ?>
    

    If you’re returning the label then this would be what you’d use

    
    <?php $pets = get_field_object('hh_facilities'); 
    if( in_array('Pet Friendly', $pets) ): ?><i title="Pet Friendly" class="fa fa-paw" aria-hidden="true"></i><?php endif; ?>
    

    if you’re returning an array

    
    <?php $pets = get_field_object('hh_facilities'); 
    if( isset($pets['pet']) ): ?><i title="Pet Friendly" class="fa fa-paw" aria-hidden="true"></i><?php endif; ?>
    
  • I’ve changed it to an array, but I never thought of the $pets[‘pet’] option.
    Thanks mate, I’ll try it in the morning after some Coffee.

    Really appreciate the help

    webecho

  • No luck with that either?

    I’m using;

    <?php $pets = get_field_object('hh_facilities');
    if( isset($pets['pet']) ): ?><i title="Pet Friendly" class="fa fa-paw" aria-hidden="true">Pet</i><?php endif; ?> 

    I’ve done a var_dump and get (array of selected options):

    [14]=>
        array(2) {
          ["value"]=>
          string(3) "pet"
          ["label"]=>
          string(12) "Pet Friendly"

    and a second array (which looks to be every available option):

    ["pet"]=>
        string(12) "Pet Friendly"

    I’ve been through all the Pet Friendly properties and re-checked the Pet Friendly box as they were unchecked after changing to pet : Pet Friendly (was previously just Pet Friendly) in Choices.

    If i use !isset, then the paw shows up on all of them.
    So according to this – they are not set as having ‘pet’ checked?

    Thoroughly confused…

  • Hi John
    I ended up ‘cheating’ – I just created a true/false field for Pet Friendly.

    Not ideal as I would like to use other Facilities conditionally – things like Beachfront, Swimming Pool etc.
    I guess I can just make them true/false as well as there wont be many.

    Really appreciate all your help … I’m going to keep trying to figure it out as it’s annoying me now ha ha

    cheers

    webecho

Viewing 18 posts - 1 through 18 (of 18 total)

The topic ‘Show icon if checkbox is checked’ is closed to new replies.