• dcexc

    (@dcexc)


    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

    <?php function show_global_pics_in_personal_library(){
        
    $variablename = get_field('dcfg_global_item_select');?>
    
        <div class="container">
            
            <?php foreach($variablename as $variablename2):?>
    
                    <a>ID);?>">
                        <center>
                        <img />ID, 'medium');?>">
                        </center>
                    </a>
    
            <?php endforeach;?>
            
            <?php $variablename = get_field('dchdl_global_item_select');?>
        
                <?php foreach($variablename as $variablename2):?>
    
                            <a>ID);?>">
                                <center>
                                <img />ID, 'medium');?>">
                                </center>
                            </a>
    
                <?php endforeach;?>
            
            <?php $variablename = get_field('dcstd_global_item_select');?>
        
                <?php foreach($variablename as $variablename2):?>
    
                            <a>ID);?>">
                                <center>
                                <img />ID, 'medium');?>">
                                </center>
                            </a>
    
                <?php endforeach;?>
            
            <?php $variablename = get_field('dcthdl_global_item_select');?>
        
                <?php foreach($variablename as $variablename2):?>
    
                            <a>ID);?>">
                                <center>
                                <img />ID, 'medium');?>">
                                </center>
                            </a>
    
                <?php endforeach;?>
            
            <?php $variablename = get_field('drcfg_global_item_select');?>
        
                <?php foreach($variablename as $variablename2):?>
    
                            <a>ID);?>">
                                <center>
                                <img />ID, 'medium');?>">
                                </center>
                            </a>
    
                <?php endforeach;?>
            
            <?php $variablename = get_field('drchdl_global_item_select');?>
        
                <?php foreach($variablename as $variablename2):?>
    
                            <a>ID);?>">
                                <center>
                                <img />ID, 'medium');?>">
                                </center>
                            </a>
    
            <?php endforeach;?>
            
            <?php $variablename = get_field('drcstd_global_item_select');?>
        
                <?php foreach($variablename as $variablename2):?>
    
                            <a>ID);?>">
                                <center>
                                <img />ID, 'medium');?>">
                                </center>
                            </a>
    
            <?php endforeach;?>
    
        </div>
    
    <?php }?>
    
    <?php add_shortcode('show_global_pics_in_personal', 'show_global_pics_in_personal_library');
    • This topic was modified 3 years ago by bcworkz.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The code depends on what makes the foreach code invalid. What does get_field() return when we shouldn’t do a foreach on it? An empty array? If so, you could do something like

    if ( 0 == count( $variablename )) {
      foreach ( $foo as $bar ) {
        // do something
      }
    }
    Thread Starter dcexc

    (@dcexc)

    function show_global_pics_in_personal_library(){?>
    	
    	
    	<?php $variablename = get_field('dcfg_global_item_select');
    
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br><br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    
    	   
    		
    		<?php $variablename = get_field('dchdl_global_item_select');
    	
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    				
    				
    				
    				
    				
    		
    		<?php $variablename = get_field('dcstd_global_item_select');
    	
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    				
    				
    				
    				
    		
    		<?php $variablename = get_field('dcthdl_global_item_select');
    	
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    				
    				
    				
    				
    		
    		<?php $variablename = get_field('drcfg_global_item_select');
    	
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    				
    				
    				
    				
    		
    		<?php $variablename = get_field('drchdl_global_item_select');
    	
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    				
    				
    				
    				
    		
    		<?php $variablename = get_field('drcstd_global_item_select');
    	
    		if ( !empty( $variablename ) ) {?>
    
    				<?php foreach($variablename as $variablename2):?>
    					<h6>
    						<a href="<?php echo get_page_link($variablename2 ->ID);?>">
    							<center>
    								<img src="<?php echo get_the_post_thumbnail_url($variablename2->ID, 'medium');?>">
    								<br>
    								<?php echo $variablename2 ->post_title; ?>
    							</center>
    						</a>
    					</h6>
    						
    					<?php $post = &get_post($variablename2->ID);
    							$post_type = $post->post_type;
    							$taxonomies = get_object_taxonomies($post_type);?>
    
    						<?php foreach ($taxonomies as $taxonomy):
    
    							$terms = get_the_terms( $variablename2->ID, $taxonomy );
    
    								foreach ( $terms as $term )?>
    									<h6>
    										<a href="<?php echo get_term_link($term->slug, $taxonomy);?>">
    											<center>
    												<?php echo $term->name;?>
    											</center>
    										</a>
    									</h6>
    								<?php endforeach;?>
    														   
    					<?php endforeach;
    						}?>
    
    <?php }?>
    
    <?php add_shortcode('show_global_pics_in_personal', 'show_global_pics_in_personal_library');
    • This reply was modified 3 years ago by bcworkz.
    Thread Starter dcexc

    (@dcexc)

    apologies – my initial upload didnt render the snippet properly. hopefully this works. it shows the loops. i got it to work – by using this if statement – which essentially sort of followed on from what you were saying

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Block error message in foreach loop when looping through ACF field’ is closed to new replies.