0

I have 2 Custom Post Types (created with CPT UI and ACF) "Global" and "Personal". The idea is for the user to reference a "global" post by displaying the image (linked) of that global post in the "personal" post that they're creating.

The loop works, when it reaches the field that contains the image info, it displays the image. However the code continues to run and displays an error like "invalid foreach()" etc etc. I basically need an "if" statement maybe or something that tells the loop to stop when it finds the info its looking for. or any other suggestions would be HUGELY appreciated!

example - 'Global' posts could be all different "pizza bases" then the user wants to create their own pizza, so they choose a certain pizza base from 'global' which displays in an ACF Relationship Field.

They do this by creating a 'Personal' Post where they can add all the toppings onto their selected base (via other acf fields), then once created, that becomes their 'personal' pizza stored as the 'personal' post type.

Hopefully this makes sense!

code as follows

function show_global_pics_in_personal_library(){
    $variablename = get_field('dcfg_global_item_select'); ?>
    <div class="container">
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field('dchdl_global_item_select'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field('dcstd_global_item_select'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field('dcthdl_global_item_select'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field('drcfg_global_item_select'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field('drchdl_global_item_select'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
        
        <?php $variablename = get_field('drcstd_global_item_select'); ?>
        <?php foreach($variablename as $variablename2) : ?>
        <a href="<?php echo get_page_link($variablename2 ->ID); ?>">
            <center>
                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium'); ?>">
            </center>
        </a>
        <?php endforeach; ?>
    </div>
    <?php
}

add_shortcode('show_global_pics_in_personal', 'show_global_pics_in_personal_library');
0

For those who are looking for an answer - after trial and error i found the "if" statement i was looking for. Full code as follows:

    
    $variablename = get_field('dcfg_global_item_select');

        if ( !empty( $variablename ) ) {?>

            <div class="container">

                <?php foreach($variablename as $variablename2):?>

                        <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                            <center>
                            <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                            </center>
                        </a>

                <?php endforeach;
                                       }?>
        
        <?php $variablename = get_field('dchdl_global_item_select');
    
            if ( !empty( $variablename ) ) {?>
                
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>
        
                        <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                            <center>
                            <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                            </center>
                        </a>

                    <?php endforeach;
                                            }?>
        
        <?php $variablename = get_field('dcstd_global_item_select');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field('dcthdl_global_item_select');
    
            if ( !empty( $variablename ) ) {?>

                <div class="container">

                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field('drcfg_global_item_select');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field('drchdl_global_item_select');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>
        
        <?php $variablename = get_field('drcstd_global_item_select');
    
            if ( !empty( $variablename ) ) {?>
                    
                <div class="container">
        
                    <?php foreach($variablename as $variablename2):?>

                            <a href="<?php echo get_page_link($variablename2 ->ID);?>">
                                <center>
                                <img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
                                </center>
                            </a>

                    <?php endforeach;
                                           }?>


    </div>

<?php }?>

<?php add_shortcode('show_global_pics_in_personal', 'show_global_pics_in_personal_library'); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.