Search code examples
phpwordpressadvanced-custom-fieldscustom-post-type

the_field - ACF not working in 'while' loop


Main string of code (that doesn't work): <span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>

 <?php 

 $args = array(
  'post_type' => 'tariffs',
  'posts_per_page' => 3,
  'type_of_site' => 'landing_page_type',

 );

 $webTariffs = new WP_Query($args);

 while ($webTariffs->have_posts()) {
  $webTariffs->the_post();?>

  <div class="pricing-item">
    <div class="pricing-item-header">

      <h3><?php the_title() ?></h3>
      <span class="price-in-kune"><?php the_field('tariff_price_kn') ?> kn</span>

    </div>
  </div>
<?php } ?>

enter image description here

The ways i tried to solve this problem:

  1. put insted of the_field('tariff_price_kn') - echo('hi') - the code worked
  2. add $post_id -> the_field('tariff_price_kn', $post_id)
  3. $currencyKune = get_field('tariff_price_kn'); And then echo $currencyKune

P.S 3) i don't know exactly where should i put $currencyKune = get_field('tariff_price_kn'), so i put it before while at first time - doesn't work and at the second time i put it after $webTariffs->the_post();


Solution

  • Try this :

                <?php 
                $args = array( 
                'post_type' => 'tariffs',
                'posts_per_page' => 3
                );
                $the_query = new WP_Query( $args );
                ?>
                <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <div class="col-sm-6">
    
                <h2 class="the-title"><?php the_field('tariff_price_kn', $post->ID); ?> +  <?php the_title() ;?> </h2>
    
    
                </div>
    
                <?php endwhile; else: ?> Nothing here <?php endif; ?>
                <?php wp_reset_query(); ?>