Support

Account

Home Forums General Issues Display Options Page repeater field in header.php

Solved

Display Options Page repeater field in header.php

  • I have a Repeater Field Group on an Options Page called “Address Book”. I would like to display the Phone Number (text field), of my first entry, in the WordPress Header.php.

    I can’t find any documentation on how to do this.

    I found something like this in the support forums but it’s not working.

    <?php
    $rows = get_field(‘location’, ‘option’); // get all the rows
    $first_row = $rows[0]; // get the first row
    $first_row_phone = $first_row[‘location_phone’ ]; // get the sub field value
    ?>
    <?php echo $first_row_phone[0]; ?>

  • You are attempting to get the index value of a string here. To explain:

    The following code results in an array:
    $rows = get_field(‘location’, ‘option’);

    Then you are using the 0 index to get the first value of that array, which will result in yet another array, only this time associative:
    $first_row = $rows[0]

    Finally, you are able to target the actual field you are looking for by it’s field label, in this case ‘location_phone’:
    $first_row_phone = $first_row[‘location_phone’ ];

    What results from that line of code is the string value you are looking for. By echoing out echo $first_row_phone[0]; you are confusing PHP into trying to get the index value of a string. So really, all you need to do is echo the string itself: echo $first_row_phone;

    Long story short, remove the ‘[0]’ from the echo.

  • That worked. And thanks for the detailed response. Much appreciated.

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

The topic ‘Display Options Page repeater field in header.php’ is closed to new replies.