• Resolved Daniel

    (@dmzayasplus)


    I am an amateur web developer, and for my blog, I want to use sets in a repeater to trigger specific images to display when checked. I am using it to create a badge system, so this repeater container would have badge images a,b,c,d checked and the next repeated container would have a,c,e… hopefully that makes sense.

    I was wondering if you might help me with the conditional statement that would allow me to do this.

    Thanks in advance and great work on the plugin!

    https://www.ads-software.com/plugins/ultimate-fields/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Radoslav Georgiev

    (@radogeorgiev)

    Hi dmzayasplus,

    I want to help you but I can’t really understand your request. Could you please give me some examples of a,b,c,d and a,c,e..? I fail to understand that is what.

    If you have managed to configure the fields as your preference, but you have issues with processing them, can you post here or send me the exported container? Both XML/PHP would work.

    Thread Starter Daniel

    (@dmzayasplus)

    I am using a repeater in your plugin. Within this repeater’s repeater fields I am using a checkbox set consisting of about 10 checkbox options. So far I’ve completed the code below:

    <?php foreach( get_uf( 'Kickstarters' ) as $Kickstarters ): extract( $Kickstarters ) ?>
    				<div class="col-md-6">
    					<h3><a href="<?php echo $kickstarter_url ?>" target="_blank"><?php echo $Name ?></a></h3>
    					<p><?php echo $kickstarter_plug ?></p>
    				</div>
    
    				<div class="col-md-6">
    					<iframe frameborder="0" height="300" scrolling="no" src="<?php echo $kickstarter_url ?>/widget/card.html?v=2" width="220"></iframe>
    				</div>
    
    				<div class="clearfix"></div>
    
    				<h3>In this Kickstarter:</h3>
    
    				<div class="badges">
    				/** I NEED THIS DIV TO APPEAR ONLY IF I HAVE CLICKED THE CHECKBOX WITH THE SLUG "20_or_less" **/
    					<div class="col-md-3">
    						<img src="https://meeplemechanic.com/wp-content/uploads/2014/09/20-or-Less-Badge.png" alt="20 or Less Badge" width="100%" />
    					</div>
    				</div>
    
    			<?php endforeach ?>
    Thread Starter Daniel

    (@dmzayasplus)

    From what I know about ACF it probably needs to be in an array that calls something like

    /*
    *  Conditional statement (Checkbox rvalue is an array)
    */
    
    if( in_array( 'red', get_field('field_name') ) )
    {
        //...
    }

    Or

    /*
    *  Query posts for a checkbox value.
    *  This method uses the meta_query LIKE to match the string "red" to the database value a:2:{i:0;s:3:"red";i:1;s:4:"blue";} (serialized array)
    *  The above value suggests that the user selected "red" and "blue" from the checkbox choices
    */
    
    $posts = get_posts(array(
        'meta_query' => array(
            array(
                'key' => 'field_name', // name of custom field
                'value' => '"red"', // matches exaclty "red", not just red. This prevents a match for "acquired"
                'compare' => 'LIKE'
            )
        )
    ));
    
    if( $posts )
    {
        //...
    }
    Plugin Author Radoslav Georgiev

    (@radogeorgiev)

    Hi again dmzayasplus,

    in the message before the last one you almost got it right. The only thing you needed to do was add the conditional statement around the badge. I tried code that was almost the same and it works like this:

    <?php foreach( get_uf( 'Kickstarters' ) as $Kickstarters ): extract( $Kickstarters ) ?>
    	<div class="col-md-6">
    		<h3><a href="<?php echo $kickstarter_url ?>" target="_blank"><?php echo $Name ?></a></h3>
    		<p><?php echo $kickstarter_plug ?></p>
    	</div>
    
    	<div class="col-md-6">
    		<iframe frameborder="0" height="300" scrolling="no" src="<?php echo $kickstarter_url ?>/widget/card.html?v=2" width="220"></iframe>
    	</div>
    
    	<div class="clearfix"></div>
    
    	<h3>In this Kickstarter:</h3>
    
    	<div class="badges">
    		<?php if( in_array( '20_or_less', $set_key ) ): ?>
    		<div class="col-md-3">
    			<img src="https://meeplemechanic.com/wp-content/uploads/2014/09/20-or-Less-Badge.png" alt="20 or Less Badge" width="100%" />
    		</div>
    		<?php endif ?>
    	</div>
    
    <?php endforeach ?>

    The only thing you need to do is replace ‘set_key’ with the ID of the set you created.

    When you extract the group ( extract( $Kickstarters ) ) the array with the checked values becomes available to you as a variable ($set_key). This variable contains the keys of the checked items from the set.

    Hope this works for you,
    Radoslav

    Thread Starter Daniel

    (@dmzayasplus)

    Worked like a charm. I owe you a five star and a resolved issue tag. Thanks a lot!

    Thread Starter Daniel

    (@dmzayasplus)

    You should put that somewhere in a FAQ or something that using the extract name creates $ keys for the whole container and can be used almost like shortcodes within template design. Thanks again.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘A question about sets in repeaters’ is closed to new replies.