skip to Main Content

I’m trying to use a repeater field and can’t seem to get it to work. I think it’s an issue with my if statement because if I remove the while loop and try echo out anything from <?php if( have_rows($aboutInfo['cards']): ?> I get nothing. I’ve tried without the ID, and a hardcoded ID as the 2nd param. Also, just to test I did <?php if( !have_rows($aboutInfo['cards']): ?> and was able to get something to echo out.

The print_r above the if statement displays the array.

<?
 /*
 Template Name: 01-Homepage
 */
 get_header(); ?>
 <? $aboutInfo = get_field( 'about' ) ?>
 <?$postid = get_the_ID(); ?>
 <div class="row">
   <div class="columns small-12 medium-7">
     <h2>
       <?= $aboutInfo['title'] ?>
     </h2>
     <p class="lead"> <?= $aboutInfo['content'] ?></p>
     <pre><?php print_r($aboutInfo['cards']) ?></pre>
     <?php if( have_rows($aboutInfo['cards'], $postid) ): ?>
       <?php while(have_rows($aboutInfo['cards'])) : the_row(); ?>
       <?php $image = get_sub_field('image') ?>
       <p><?= $image['url'] ?></p>
       <?php endwhile; ?>
     <?php endif; ?>
   </div>
 </div>
 <?php get_footer(); ?>

Here is what my ACF looks like
enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    The issues was that I created a group called "about" and the "cards" were nested in that group and to access that field I needed to use "about_cards".

    <?
     /*
     Template Name: 01-Homepage
     */
     get_header(); ?>
    ​
     <?php while ( have_posts() ) : the_post();
    ​
        // group field
        $about = get_field( 'about' ); 
        
        if ( !empty( $about ) ) { ?>
    ​
            <div class="row">
                <div class="columns small-12 medium-7">
    ​
                <?php if ( !empty( $about['title'] ) ) { ?>
                    <h2><?php echo esc_html( $about['title'] ); ?></h2>
                <?php }
                
                if ( !empty( $about['content'] ) ) { ?>
                    <p class="lead"><?php echo wp_kses_post( $about['content'] ); ?></p>
                <?php }
    ​
                if( have_rows( 'about_cards' ) ) : // repeater
    ​
                    while ( have_rows( 'about_cards' ) ) : the_row(); 
    ​
                        $about_card_image   = get_sub_field('image');
                        $about_card_title   = get_sub_field('title');
                        $about_card_content = get_sub_field('content');
    ​
                        if ( !empty( $about_card_image ) ) {
                            echo wp_get_attachment_image( $about_card_image, 'medium' );
                        }
    ​
                        if ( !empty( $about_card_title ) ) {
                            echo '<h3>' . esc_html( $about_card_title ) . '</h3>';
                        }
    ​
                        if ( !empty( $about_card_content ) ) {
                            echo '<p>' . esc_html( $about_card_content ) . '</p>';
                        } ?>
    ​
                    <?php endwhile;
                endif; ?>
                </div>
            </div>
    ​
        <?php } // about field not empty ?>
    ​
    <?php endwhile; // End of the loop. ?>
    ​
    <?php get_footer(); ?>  
    

  2. I think you are doing it wrong. There are so many bugs in your code. check
    https://www.advancedcustomfields.com/resources/group/ and have_rows() the first param need to be selector. check below code.

    <?php 
    
    /* Template Name: 01-Homepage */
    
    get_header(); 
    
    $aboutInfo = get_field( 'about' );
    $postid    = get_the_ID();
    
    if( have_rows('about') ): 
    
        $title   = get_sub_field('title');
        $content = get_sub_field('content');
    
        ?>
    
        <div class="row">
    
            <div class="columns small-12 medium-7">
    
                <?php while( have_rows( 'about' ) ): the_row(); ?>
    
                    <h2><?php echo $title; ?></h2>
                    <p class="lead"><?php echo $content; ?></p>
                    
                    <?php if( have_rows( 'cards' ) ): while( have_rows( 'cards' ) ) : the_row(); ?>
    
                            <?php $image = get_sub_field( 'image' ); ?>
    
                            <img src="<?php echo $image['url']; ?>" />
    
                    <?php endwhile; endif;
    
                endwhile; ?>    
    
            </div>
    
        </div>
    
    <?php endif;
    
    get_footer(); ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search