Skip to content
Advertisement

ACF wordpress Modulo variable

I have some code. Trying to add ACF filed(number) instead static number

    <?php $counter = 1; ?>
<?php $fff = the_field('show_ad_every_x_posts', 'option'); ?>
  <?php if( $index_query->have_posts() ) : while( $index_query->have_posts() ) : $index_query->the_post(); ?>
<?php if($counter % $fff == 0 ): ?>

So when I save this I have error like Fatal error: Uncaught DivisionByZeroError: Modulo by zero

When I change <?php $fff = the_field('show_ad_every_x_posts', 'option'); ?> to <?php $fff = 7; ?> It works fine!

The problem is that Im trying to display ADs after every X post. But I think my code will replase post to ad.

Here is full code

<?php $index_query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => '13', 'order' => 'DESC' ) ); ?>
<?php $counter = 1; ?>
<?php $fff = the_field('show_ad_every_x_posts', 'option'); ?>
  <?php if( $index_query->have_posts() ) : while( $index_query->have_posts() ) : $index_query->the_post(); ?>
<?php if($counter % 7 == 0 ): ?>
  <div class="item">
        <div class="meme-card-body">
          <img src="<?php the_field('ads_masonry_banner', 'option'); ?>" class="img-home-posts">
      </div>
  </div>
  <?php else : ?>
    <div class="item">
        <a href="<?php the_permalink(); ?>">
          <div class="meme-card-body">
          <?php if(has_post_thumbnail()): ?>
            <img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" class="img-home-posts">
          <?php endif; ?>
          <h1 class="meme-title"><?php the_title(); ?></h1>
        </div>
        </a>
    </div>
<?php endif; ?>
  <?php $counter++; endwhile; else : endif; ?>

So how to resolve ths question?

Advertisement

Answer

<?php $fff = the_field('show_ad_every_x_posts', 'option'); ?>

the_field displays the value directly and does not have a return value – so you assigned “nothing” to $fff here.

You want to use get_field – that one returns the value.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement