• Resolved reedenj

    (@reedenj)


    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:

    [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 1 replies (of 1 total)
  • Plugin Support Paul Clark

    (@pdclark)

    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() or PHP templates.

    [linked_cars]

    <?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();
    }
    );
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.