Support

Account

Home Forums Add-ons Repeater Field Get all repeater images from all posts but only from 3 categories

Helping

Get all repeater images from all posts but only from 3 categories

  • THE BACKGROUND

    1. Custom Post Type = “contestants”
    2. Custom Taxonomy for contestants = “year_type”
    3. Contestants can be in only one of many different year_types.
    4. Each contestant has a repeater field for sponsor logo and sponsor URL.

    THE PROBLEM

    For the homepage, I need to get all the sponsor logos from all the posts that are in one of 3 year_type categories. I can’t seem to get this part to work.

    CURRENT SINGLE CODE

    This is the code on the single-contestant.php template that shows the sponsor logos for each contestant.

    <div class="sponsors">
    <?php
    // check if the repeater field has rows of data
    if( have_rows('sponsors') ):
        // loop through the rows of data
    
        while ( have_rows('sponsors') ) : the_row();
    
            // display a sub field value
            $image = get_sub_field('sponsor_logo');
            $url = get_sub_field('sponsor_url');
            $name = get_sub_field('sponsor_name');
    
            ?>
            <div class="sponsor"><a href="<?php echo $url; ?>" target="_blank" title="<?php echo $name; ?>"><img src="<?php echo $image; ?>" /></a></div>
        <?php endwhile;
    
    else :
        // no rows found
    endif;
    ?>
    </div>
  • I figured this out. In case anyone else wants to know…

    <div class="sponsors">
    <?php
    global $post;
    $the_query = new WP_Query( array(
        'post_type' => 'contestants',
        'tax_query' => array(
        array(
          'taxonomy' => 'year_type',
          'field' => 'slug',
          'terms' => array( 'term_1', 'term_2', 'term_3' )
        )
      ))
    ) ;
    
    if($the_query->have_posts()){
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
    if( have_rows('sponsors', $post->ID) ):
        // loop through the rows of data
    
        while ( have_rows('sponsors') ) : the_row();
    
            // display a sub field value
            $image = get_sub_field('sponsor_logo');
            $url = get_sub_field('sponsor_url');
            $name = get_sub_field('sponsor_name');
    
            ?>
            <div class="sponsor"><a href="<?php echo $url; ?>" target="_blank" title="<?php echo $name; ?>"><img src="<?php echo $image; ?>" /></a></div>
        <?php endwhile;
    
    else :
        // no rows found
    endif;
    }
    }
    ?>
    </div>

    Please mark this thread closed. Thank you.

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

The topic ‘Get all repeater images from all posts but only from 3 categories’ is closed to new replies.