Reputation: 227
Below is a script that I'm still unsure on how to get it to work, in Wordpress I have a repeater field that I can input the number of days that are in a month, so it creates calendar squares for me to highlight in a booking process.
What I want to do, is to have the field 'how_many_days' to run a loop that will then repeat the number of divs calendarPost. So Ideally I can input separate number of loops.
A live version of the output is here: http://universitycompare.com/school-bookings/
<?php if(get_field('calendar_repeater_field')): ?>
<?php while(has_sub_field('calendar_repeater_field')): ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php $wp_query->query('showposts='.$numberoffields ); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
<?php endwhile; ?>
<?php endif; ?>
FYI - I didn't know whether this would be a PHP related problem or WP only, so please advise if it this post should be elsewhere and I will remove and repost in the correct stackoverflow forum.
Upvotes: 0
Views: 80
Reputation: 549
Your question didn't completly explain if you were in fact trying to output posts so below is a couple of suggesstions.
I'll start with what I think you're trying to do:
If you're' just wanting to output the div.calendarPost over and over (based on the number of days) then you don't need a WordPress loop for that. A standard PHP for loop will do
<?php if ( get_field('calendar_repeater_field' ) ) : ?>
<?php while ( has_sub_field('calendar_repeater_field' ) ) : ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php for ( $i=0; $i < $numberoffields; $i++ ) { ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
If however you're wanting to output posts (based on the number of days in the ACF field) then you would use the below code.
<?php if ( get_field('calendar_repeater_field' ) ) : ?>
<?php while ( has_sub_field('calendar_repeater_field' ) ) : ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php $calendar_posts = new WP_Query('posts_per_page=' . $numberoffields); ?>
<?php if ( $calendar_posts->have_posts() ) : while ( $calendar_posts->have_posts() ) : $calendar_posts->the_post(); ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Refer to "The Usage" section of the WP Codex for more info: http://codex.wordpress.org/Class_Reference/WP_Query.
Hope that helps.
Upvotes: 1