WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Conditional Statement (Advanced Custom Fields) (8 posts)

  1. cmdshiftdesign
    Member
    Posted 2 years ago #

    Using the Advanced Custom Fields plugin with the Repeater add-on. (http://www.advancedcustomfields.com)

    I have repeated field called "lists' and within that a sub field called 'spcl'

    I want a conditional statement w/in my template saying .... IF 'spcl' is there,
    DO THIS
    if not
    DO NOTHING

    since i am dealing with a subfield, im having trouble getting this to check for first the list field and then the sub field.. this is what i have right now...

    <?php
    if(get_field('lists') && get_sub_field('spcl'))
    {
    	echo '<p>POOP!</p>';
    }
    ?>
  2. Andrew Norcross
    Member
    Posted 2 years ago #

    While I've never heard of the plugin before now, their documentation is pretty straight forward. Give this a shot:

    <?php
    
    $lists	= get_field('lists');
    $spcl 	= get_sub_field('spcl');
    
    if( ($lists) && ($spcl) )
    {
    	echo '<p>POOP!</p>';
    }
    ?>
  3. cmdshiftdesign
    Member
    Posted 2 years ago #

    Thanks, but no dice.

    w/ my solution and this i can get it to work if i cam only calling for the 'lists' field, but once i also ask for the sub_field ....nope :/ GRF!

    Again, thanks though!

  4. Andrew Norcross
    Member
    Posted 2 years ago #

    Without seeing the rest of the code, I'd say that since it's a repeating field, it's probably being stored in an array and thus has to be broken out with a foreach.

  5. chriseverson
    Member
    Posted 2 years ago #

    What do you get when you try to echo out the conditional functions?

    <?php
    
    echo get_field('lists');
    echo get_sub_field('spcl');
    
    ?>

    Do you get values for both? Also, does it maybe not process the HTML markup? Try removing the paragraph tags?

    Good luck lady! Feel free to shoot me credentials via email or whatever if you don't figure it out within a couple hours. Should be home from work by then and available to toy with it.

  6. You're gonna want to look at this documentation, as you're on a repeater field (which is inherently different):

    http://www.advancedcustomfields.com/docs/code-examples/#wpshdo_7

    So you'd do something like ...

    $lists = get_field( 'lists' );
    if( $lists ){
    	foreach( $lists as $list ){
    		if( $list['spcl'] ){
    			echo '<p>POOP!</p>';
    		}
    	}
    }

    it should trigger once for each instance of the repeater field, if spcl isn't blank

  7. cmdshiftdesign
    Member
    Posted 2 years ago #

    Hey Dudes! Was JUST now able to get back to working on this and George, you win today!

    Thanks to all of you! <3

  8. chriseverson
    Member
    Posted 2 years ago #

    Oh yeah, that too. George wins!

Topic Closed

This topic has been closed to new replies.

About this Topic