Title: Nested each in shortcode
Last modified: April 19, 2025

---

# Nested each in shortcode

 *  Resolved [reedenj](https://wordpress.org/support/users/reedenj/)
 * (@reedenj)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/nested-each-in-shortcode/)
 * Hello,
   I’m using shortcode display values from relationship field images, and
   I would like to display their relationship field as well. It semi-works, but 
   values are duplicated.
 * My code:
 *     ```wp-block-code
       [each linked_car] {@linked_car._img} {@post_title} [each linked_car.sub-color]   {@linked_car.sub-color._img}   {@post_title} [/each][/each]
       ```
   
 * `"linker_car"` field is a relationship field (file/image/video) of my pod called“
   cars”
 * `"sub-color"` field is a relationship field (file/image/video) that extends Media
   content type
 * The shortcode above works if there is only one value for `"linked_car"`. However,
   if I choose multiple values (images), all sub-color values are duplicated for
   each `linked_car` entry. Using nested each without `"linked_car"` part doesn’t
   show anything.
 * Is there a way how to display `"sub-color"` values only for their correct and
   one `"linked_car"`, and not for every entry?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Support [pdclark](https://wordpress.org/support/users/pdclark/)
 * (@pdclark)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/nested-each-in-shortcode/#post-18448347)
 * Field name within `[each]` should not be repeated as a field prefix within the`[
   each]` like `{@linked_car.xxx}` within an `[each linked_car]`
 * `[each]` template tags cannot be nested due to the nature of templating shorthand.
 * For more complex logic available in a full programming language, such as nested
   loops, use [add_shortcode()](https://developer.wordpress.org/reference/functions/add_shortcode/)
   or PHP templates.
 * `[linked_cars]`
 *     ```wp-block-code
       <?php/** * Plugin Name: Linked Cars shortcode * Description: <code>[linked_cars]</code> */ add_shortcode( 	'linked_cars', 	function( $atts, $content, $tagname ) { 		ob_start(); 		foreach( (array) pods()->field( 'linked_car' ) as $linked_car_id ) { 			// https://docs.pods.io/code/pods/find/ 			$car = pods( 'car', $linked_car_id ); 			// https://docs.pods.io/code/pods/template/ 			echo $car->template( 				null, 				'{@_img}{@post_title}[each sub-color]	{@_img}	{@post_title}[/each] 				', 			); 		} 		return ob_get_clean(); 	});
       ```
   
 *  Thread Starter [reedenj](https://wordpress.org/support/users/reedenj/)
 * (@reedenj)
 * [1 year, 1 month ago](https://wordpress.org/support/topic/nested-each-in-shortcode/#post-18453443)
 * Thanks for the reply. I tried it, and it lists 15 rather random files/images,
   not the ones linked in `linked_car` field.
 * sql debug
 *     ```wp-block-code
        SELECT                                DISTINCT                t.*                FROM wp_posts AS t                                WHERE ( ( t.post_status IN ( 'publish' ) ) AND ( t.post_type = 'car' ) )                                                ORDER BY t.menu_order, t.post_title, t.post_date                LIMIT 0, 15
       ```
   
 *  Plugin Support [pdclark](https://wordpress.org/support/users/pdclark/)
 * (@pdclark)
 * [1 year ago](https://wordpress.org/support/topic/nested-each-in-shortcode/#post-18496627)
 * Specify the exact post the linked_cars field should be read from:
 *     ```wp-block-code
       foreach( (array) pods( 'post', get_the_ID() )->field( 'linked_car' ) as $linked_car_id ) {
       ```
   
 * …where `post` is the post type and `get_the_ID()` should return the ID of the
   post with the linked_cars field if within a global loop.
 * [https://docs.pods.io/code/pods/](https://docs.pods.io/code/pods/)
 * If `get_the_ID()` is not returning the ID of the expected post, configure it 
   explicitly or consider the context in which the shortcode is being used

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Nested each in shortcode’ is closed to new replies.

 * ![](https://ps.w.org/pods/assets/icon.svg?rev=3286397)
 * [Pods - Custom Content Types and Fields](https://wordpress.org/plugins/pods/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pods/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pods/)
 * [Active Topics](https://wordpress.org/support/plugin/pods/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pods/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pods/reviews/)

## Tags

 * [nested](https://wordpress.org/support/topic-tag/nested/)

 * 3 replies
 * 2 participants
 * Last reply from: [pdclark](https://wordpress.org/support/users/pdclark/)
 * Last activity: [1 year ago](https://wordpress.org/support/topic/nested-each-in-shortcode/#post-18496627)
 * Status: resolved