Support

Account

Home Forums General Issues Display CPT field loop based on checked values in a list

Unread

Display CPT field loop based on checked values in a list

  • My first CPT has a checkbox field ‘product’ with choices – Choice1 and Choice2

    I created a 2nd CPT and have created 2 pages Choice1Post and Choice2Post with a custom_field_title field. if i check (Choice1) in my 1st CPT, i want to call the Choice1Post ID and display the custom_field_title into my template.

    If i check Choice1 and Choice2 I want to loop through and display the custom_field_title from the Choice1Post and Choice2Post in a row.

    This is what i have come up with so far below. I am about a 3 out of 10 for PHP skill level.

    What is happening with the below code, is that i have Choice1 and Choice2 checked, but it is calling in the the custom_field_title from Choice1Post twice in a row.

    I want this to display in the frontend:
    Choice 1 Custom Field Title
    Choice 2 Custom Field Title

    Instead of what’s displaying now:
    Choice 1 Custom Field Title
    Choice 1 Custom Field Title

    <?php
    $fields = get_field('product');
    if( $fields ): ?>
    <?php foreach( $fields as $field ): 
        if(in_array( "Choice1", get_field('product')) ) { 
            $productinfo = 120;
        }
        elseif(in_array( "Choice2", get_field('product')) ) { 
            $productinfo = 121;
        };
          ?>
            <p><?php the_field('custom_field_title', $productinfo); ?></p>
        <?php endforeach; ?>
    <?php endif; ?>
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.