• Resolved hephaistos

    (@hephaistos)


    I am trying to display the first value of a field in a PHP page template, but did not succeed. My code is as follows:

    <?php
    $compositions = pods('composition');
    echo $compositions->display('cb-instrument', true);
    ?>

    cb-instrument is a repeatable text field. I set the $single parameter of the display() function to “true” following the function documentation, but obviously misunderstood the purose. I expected this statement to output the first entry of the field, but it still displays ALL entries.

    How can I output the first, second, nth… entry of a repeatable field? And how can I maybe detect, if a repeatable field has several entries?

    Thank you very much
    Pascal

Viewing 1 replies (of 1 total)
  • Plugin Support pdclark

    (@pdclark)

    <?php

    $instruments = pods('composition')->field( 'cb-instrument' );

    // This line might not be necessary -- depends on if the array is ID => value or Index => value.
    $instruments = array_values( $instruments );

    if ( ! empty( $instruments ) ) {
    echo $instruments[0];

    if ( array_key_exists( 1, $instruments ) ) {
    printf(
    '<p>There is a second instrument: %s</p>',
    esc_html( $instruments[1] )
    );
    }

    echo '<ul>';
    foreach( (array) $instruments as $key => $instrument ) {
    if ( 0 === $key ) {
    printf(
    '<li>The first instrument is <code>%s</code></li>',
    esc_html( $instrument )
    );
    }else {
    echo sprintf(
    '<li>Instrument %s is <code>%s</code></li>',
    number_format( $key + 1, 0 ),
    $instrument
    );
    }
    }
    echo '</ul>';
    }else {
    echo '<p>This composition is a cappella.</p>';
    }
Viewing 1 replies (of 1 total)

The topic ‘Display specific entry of repeatable field’ is closed to new replies.