Support

Account

Home Forums General Issues Accessing an image from a repeater based on post object field

Helping

Accessing an image from a repeater based on post object field

  • I have a repeater set up in my Options page called “Products.” The repeater has a text field, an image field and a post object field. Each product has it’s own child page under the parent page “Products” and in the repeater I have each product assigned to its corresponding page in the post object field. What I would like to do is add code to my page.php template that will grab the corresponding image from the repeater field depending on what page you’re on. For instance, if you are on the “Bicycle” page, then it would display the image from the repeater with “Bicycle” selected as the post object.

    How could I go about this?

    Thanks!

  • I am having a hard time working out how things are related by reading your description.

    Assuming that I understand, you would need to loop over every row of the repeater on the options page until you found the correct row. As an example

    
    $post_id = get_the_ID();
    if (have_rows('repeater_on_options_page', 'options')) {
      while (have_rows('repeater_on_options_page', 'options')) {
        the_row();
        if (get_sub_field('post_object_field', false)) { // using second argument of false here to get just the ID
          // this is the row we want, get the image field and show it
          // your code here .....
          // after we show an image there is no reason to continue
          // so break out of loop
          break;
        }
      }
    }
    

    Seems complicated, why not just put the image field on the product page instead of needing to go to a different location in the admin to edit it? Also, your setup will not scale. I have many product you are doing this on then 1st the repeater will take forever to load and second it will slow down the site as you loop over more and more rows of the repeater because a query to the options page must be done for every row. This could be somewhat fixed on the front end by auto loading the values on this options page, but then you have the potential of an out of memory condition if the repeater grows too large.

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

The topic ‘Accessing an image from a repeater based on post object field’ is closed to new replies.