Support

Account

Home Forums Front-end Issues ACF Not working inside WordPress Loop

Solving

ACF Not working inside WordPress Loop

  • I’m trying to add a text acf to a custom page template with a custom posts loop.

    There are other ACFs on the page however my ACF does not show on the front end unless I place it outside the loop. Below is my code. My call to the ACF in question is <?php the_field(‘post_archive_subheading’); ?> and it won’t work inside the loop.

    
    <?php
    /* Template Name: Archive Page Custom */
    ?>
    <?php
    /**
     * Post archive template.
     *
     * @link https://codex.wordpress.org/Template_Hierarchy
     *
     * @package XXXXXXX
     */
    
    $featured_posts = get_field( 'featured_posts', get_option( 'page_for_posts' ) );
    $not_in         = array();
    
    if ( $featured_posts && '' !== $featured_posts && 3 === count( $featured_posts ) ) :
      foreach ( $featured_posts as $featured_post ) :
        $not_in[] = $featured_post->ID;
      endforeach;
    endif;
    
    $posts_args    = array(
      'posts_per_page' => '12',
      'post__not_in'   => $not_in,
      'paged'          => get_query_var( 'paged' ) ?: 1,
    );
    $posts_archive = new WP_Query( $posts_args );
    
    get_header(); ?>
    
      <main id="main" class="site-main custom-page-content">
    
        <?php get_template_part( 'template-parts/content', 'page-single-hero' ); ?>
        <div class="entry-content page-content blog">
        <?php the_content(); ?>
    
      </div><!-- .entry-content -->
        <?php if ( has_nav_menu( 'post-categories' ) ) : ?>
    
        <nav class="post-categories-navigation" aria-label="<?php esc_attr_e( 'Post Categories Navigation', 'ventura-county-coast' ); ?>">
          <button
            id="toggle-categories"
            class="toggle-categories-button hidden"
            aria-pressed="false"
            type="button"
          >
            <?php
              esc_html_e( 'Categories', 'ventura-county-coast' );
    
              vcc_display_svg(
                array(
                  'icon'   => 'close',
                  'width'  => '16px',
                  'height' => '16px',
                  'fill'   => '#5b5f60',
                )
              );
            ?>
          </button>
          <?php
          wp_nav_menu(
            array(
              'fallback_cb'    => false,
              'theme_location' => 'post-categories',
              'menu_class'     => 'menu post-categories',
              'container'      => false,
            )
          );
          ?>
        </nav><!-- .post-categories-navigation -->
    
        <?php endif; ?>
    
        <?php if ( $featured_posts && 3 === count( $featured_posts ) && ! is_paged() ) : ?>
          <div class="featured-posts">
            <?php
            foreach ( $featured_posts as $key => $featured_post ) :
              if ( 1 === $key ) :
                ?>
                <div class="featured-end-wrap">
                <?php
              endif;
              ?>
              <div class="featured featured-<?php echo esc_attr( $key + 1 ); ?>">
              <?php
              vcc_display_card(
                array(
                  'image' => get_the_post_thumbnail_url( $featured_post->ID, 'full-width' ),
                  'title' => $featured_post->post_title,
                  'text'  => vcc_get_the_excerpt(
                    array(
                      'post' => $featured_post->ID,
                    )
                  ),
                  'class' => '',
                  'url'   => get_permalink( $featured_post->ID ),
                )
              );
              ?>
              </div><!-- .featured-# -->
    
              <?php
              if ( 2 === $key ) :
                ?>
                </div><!-- .featured-end-wrap -->
                <?php
              endif;
    
            endforeach;
            ?>
          </div><!-- .featured-posts -->
          <?php endif; // $featured_posts ?>
    
          <?php
          /* Start the Custom Loop */
          $counter = 0;
    
          if ( $posts_archive->have_posts() ) :
            ?>
            <div class="grid posts-grid">
            <?php
    
            while ( $posts_archive->have_posts() ) :
              $posts_archive->the_post();
              ?>
              <div class="col col-4 col-md-6 col-sm">
              <?php the_field('post_archive_subheading'); ?>
              <?php
              vcc_display_card(
                array(
                  'image' => get_the_post_thumbnail_url( get_the_ID(), 'full-width' ),
                  'title' => get_the_title(),
                  'text'  => vcc_get_the_excerpt(),
                  'class' => '',
                  'url'   => get_the_permalink(),
                )
              );
              ?>
    
              <?php if ( 2 === $counter ) : ?>
                </div><!-- .col.col-4 -->
                </div><!-- .grid -->
                <?php get_template_part( 'template-parts/mailchimp-newsletter' ); ?>
    
                
                <div class="grid posts-grid"><!-- Open a new .grid -->
              <?php else : ?>
                </div><!-- .col.col-4 -->
              <?php endif; ?>
    
              <?php
              $counter++;
    
            endwhile;
            wp_reset_postdata();
            vcc_display_numeric_pagination( $posts_args, $posts_archive );
            ?>
    
          </div><!-- .grid -->
        <?php
        else :
    
          get_template_part( 'template-parts/content', 'none' );
    
        endif;
        ?>
      </main><!-- #main -->
    
    <?php get_footer(); ?>
    
  • is this field

    
    <?php the_field('post_archive_subheading'); ?>
    

    on the page that is set as page_for_posts

    In side the loop ACF will be looking for the field on the current post of the current loop.

  • I ended up figuring out the issue. I had to add new vars to the header

    $archive_subheading = get_field( ‘archive_subheading’ );

    Then I had to add the following conditional formatting to trigger the ACF code inside the loop.

    <?php if( $archive_subheading ) : ?>
    <h2 class=”customsub”><?php echo $archive_subheading ?></h2>
    <?php endif; ?>

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

You must be logged in to reply to this topic.