ACF Repeater Fields different counts - php

I have a slice of code:
<?php
if( have_rows('our_people') ):
$i = 0;
while ( have_rows('our_people') ) : the_row(); ?>
<?php $i++; ?>
<?php if( $i > 3 ):
break; ?>
<?php endif; ?>
<a class="people-thumb fancybox-inline" href="#fancybox-<?php echo $i;?>">
<div class="people-thumb-image" style="width:172px;height:172px;overflow:hidden;background:transparent url(<?php the_sub_field('people_image'); ?>) no-repeat center center; background-size:cover;"></div>
<div class="people-thumb-details">
<h3><?php the_sub_field('people_name_acf'); ?></h3>
<p><?php the_sub_field('people_title'); ?></p>
</div>
</a>
<div class="peopleoverlay">
<div id="fancybox-<?php echo $i;?>">
<img src="<?php the_sub_field('people_image'); ?>" width="172" style="float:left;margin:0 10px 10px 0;"> <?php the_sub_field('people_details'); ?>
</div>
</div>
<?php endwhile;
else :
endif;
?>
What this does it simply count the entries and then stop after 3 - works fine.
However, with the other rows in the repeater field (after 3) how do I get those to also display? Reasoning is these 3 are in a seperate styling div, and all other entries are placed in another div.
However, if I try to repeat this code, nothing shows. So I must need to reset this somehow? I want all repeater rows after 3 to show in a different section.

It looks that your code specifically says not to display more than 3 results! Annotated below.
$i = 0; // Start a counter
while ( have_rows('our_people') ) : the_row(); ?> // As long as there are posts to display
<?php $i++; ?> // increment the counter
<?php if( $i > 3 ): // if the counter is 4 or more...
break; ?> // don't execute the code below; jump out of the while loop.
<?php endif; ?>
That being said, remove $i = 0, and the following lines:
<?php $i++; ?>
<?php if( $i > 3 ):
break; ?>
<?php endif; ?>
This will allow ACF to display all the people stored in that metafield.
However, if you'd like to add a different class to the 4th div and beyond, change the conditional, to remove the break statement, and to wrap the result in a div that identifies it.
<?php $i++; ?>
<?php if( $i > 3 ): ?>
<div class="other-style">
<?php endif; ?>
Then, you'd have to close it at the end with another check.
...
<?php if( $i > 3 ): ?>
</div> <!-- .other-style -->
<?php endif; ?>

Related

While loop, wrap items in a count in a separate div (index.php)

I have the following so far:
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<?php the_title();?>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<?php the_title();?> blah
<?php the_excerpt();?>
<?php endif; ?>
<?php endwhile; ?>
I want to wrap the first 4 in a '<div class="top"></div>' and the bottom 6, I want all of them wrapped in a '<div class="bottom"></div>'
Any tips of how this can be done using the while loop would be greatly appreciated
Try the below snippet.
<?php
$count = 0;
while (have_posts()) : the_post(); $count++;
if ( $count == 1 ) echo '<div class="top">';
if ( $count > 5 ) echo '</div><div class="bottom">';
the_title();
if ( $count > 5 ) the_excerpt();
endwhile;
echo "</div>"
?>
This might help you.
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<div class="top">
<?php the_title();?>
</div>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<div class="bottom">
<?php the_title();?> blah
<?php the_excerpt();?>
</div>
<?php endif; ?>
<?php endwhile; ?>

PHP split list items into 2 columns

I am running a Wordpress site, that uses individual ACF fields to store bullet point text.
<?php if( get_field('bullet_points') ): ?>
<div class="row">
<div class="col-3">
<p class="big"><?php the_field('bullet_point_text'); ?></p>
</div>
<div class="col-3">
<ul>
<?php while( has_sub_field('bullet_points') ): ?>
<?php $post_counter++; ?>
<li><?php the_sub_field('individual_bullet_point'); ?></li>
<?php
if ( 0 == $post_counter % 5 ) {
echo '</ul></div><div class="col-3"><ul>';
}
?>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endif; ?>
The above code takes the fields (bullet_points) and outputs them in multiple columns every 5 points - I hardcoded this to make sure the code works. How would I get it to output these into 2 (as evenly) spaced lists?
So, if I have 6 points, I want 3 in the first DIV and 3 in the second DIV. Similarly if I have 7 points, I want 3 in the first DIV and 4 in the second DIV.
Any help would be appreciated!
I believe this will work...
<?php $count_bullets = count(get_sub_field('bullet_points')); ?>
<?php $col_items = ceil( $count_bullets / 2 ); ?>
<?php while( has_sub_field('bullet_points') ): ?>
<?php $post_counter++; ?>
<li><?php the_sub_field('individual_bullet_point'); ?></li>
<?php
if ( 0 == $post_counter % $col_items ) {
echo '</ul></div><div class="col-3"><ul>';
}
?>
<?php endwhile; ?>

My code causing infinite loop, can't see the bug

I am working on a WordPress project, using the Advanced Custom Fields plugin.
However I am getting a crazy infinite loop from my code, so I've obviously messed things up somewhere, however after a good two hours looking at it I can't see the problem.
<?php get_header(); ?>
<?php
// If the loop has posts
if (have_posts()) :
// For each post this page has
while (have_posts()) : the_post();
// Display the Flexible Content
while(has_sub_field('add_row')):
// If the content type is Tagline
if(get_row_layout() == 'tagline'): ?>
<div class="row paddingBottom paddingTop">
<div class="col-xs-12">
<h3 class="tagLine"><?php the_sub_field('tagline'); ?></h3>
</div>
</div>
<?php
// If the content type is a Featured Image
/*elseif ( get_row_layout() == 'featured_image' ): ?>
<div class="col-xs-12 textCenter">
<?php
$attachment_id = get_sub_field('featured_image');
$size = "full-width"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</div>
<?php*/
// If the content type is a horizontal rule
elseif ( get_row_layout() == 'horizontal_rule' ) : ?>
<div class="row">
<div class="col-xs-12">
<hr />
</div>
</div>
<?php
// If the content type is Columns
elseif ( get_row_layout() == 'columns' ):
// If there is at least one column section
if(get_sub_field('column')):
// For each column section
while(has_sub_field('column')): ?>
<div class="row paddingTop paddingBottom">
<?php
if(get_sub_field('column_content_type')):
$count_rows = 0;
// Counting
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
// Loop
while(has_sub_field('column_content_type')): ?>
<div class="
<?php /*
if ( $count_rows == 1 ) :
echo "col-xs-12";
elseif ( $count_rows == 2 ) :
echo "col-xs-6";
elseif ( $count_rows == 3 ) :
echo "col-xs-6 col-sm-4";
elseif ( $count_rows == 4 ) :
echo "col-xs-6 col-sm-6 col-md-4";
endif; */
?>
"> <?php
// Title
if ( get_sub_field('title') ) : ?>
<h2 class="smallTitle"><?php the_sub_field('title');?></h2><?php
endif;
// Text Area
if ( get_sub_field('text') ) :
the_sub_field('text');
endif;
// Link
if ( get_sub_field('link') ) : ?>
<?php // eventually need to make it so you can change the link text ?>
<a class="textCenter" href="<?php the_sub_field('link'); ?>">
More info
</a> <?php
endif; ?>
</div> <?php
endwhile;
endif;
?>
</div>
<?php
endwhile; // end for each column section
endif; // end checking for at least one column section
endif; // end checking content type is columns
endwhile; // end flexible content
endwhile; // end for each post
endif; // end checking for at least one post
?>
<?php get_footer(); ?>
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
That's your problem... But it could be one of many based on what I'm seeing. It looks like you think while loops work like if statements... All you are doing in that loop is incrementing it over and over. It will never end.
I think the mistake is
// Display the Flexible Content
while(has_sub_field('add_row')):
i think you need to use if(){
// Code
}
instead of "while"

Wordpress: Most recent post in one column, other posts in two columns

My client is looking to change her homepage to look similar to this site: http://www.eatliverun.com/
Where the most recent post is up top in full in one column and the others are in two column excerpts.
She wants to have just one recent post display and the rest in the two column format. I'm just not sure how to accomplish this correctly.
Her website is located at http://pamelastable.com/
Integrating a counter into your loop can easily help you achieve this layout. Try something as follows (note: the 'six columns' class represents your 2 column layout in a 12 column grid):
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ($count == 1) { ?>
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
<?php $count++; ?>
<?php } else { ?>
<?php if($count % 2 == 0) { ?>
<div>
<? } ?>
<div class="six columns">
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
</div>
<?php if($count % 2 == 1) { ?>
</div>
<? } ?>
<?php $count++; ?>
<? } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Just add a counter to the loop and style the html differently for the first iteration.
<?php $i = 0; while ( have_posts() ) : the_post(); //inside loop ?>
<?php if ($i++ == 0) : ?>
<div>First Post</div>
<?php else: ?>
<div>Other Posts</div>
<?php endif; ?>
<?php endwhile; ?>
In your index.php or other template file^
Just use a counter to see which post number you are on.
<?php
if (have_posts()) {
$i = 0;
while (have_posts()) {
the_post();
if (++$i == 1) {
echo '<div class="entry-large">';
} else {
echo '<div class="entry-small">';
}
the_content();
echo '</div>'
}
}

PHP counter increment for every 1st and 4th div

I am running a wordpress if while statement and would like my items to display in a column format.
I am currently using 960.gs which is your standard 960 grid system, and by default adds 10px padding to left and right, by simply passing a class of alpha or omega you can get rid of these.
How do I get php to execute a statment for every 1st one to add alpha and 4th one omega?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php endwhile; endif; ?>
Try maybe something like this. I wasn't sure what you meant by every 1st and 4th so I made it the way you can see. You should be able to customize it yourself.
<?php $counter = 0; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$div = '<div class="entry';
if ($counter % 4 == 0)
$div .= " alpha";
else if($counter % 4 == 3)
$div .= " omega";
echo $div . '">';
?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php $counter++; ?>
<?php endwhile; endif; ?>
Try this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry <?php if (!($i++ % 4)) { ?>alpha<?php } ?> <?php if (!(($j++)+1 % 4)) { ?>omega<?php } ?>">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php endwhile; endif; ?>
Looks kind of like a repeat of something I've seen recently. (Not your fault, it was poorly named and you wouldn't have been able to search on it). Help needed improving a foreach loop
I think what you're looking for would be more precisely described as class=alpha on row 4k+1, class=omega on row=4k
I'm not familiar with your if/while syntax, and the embedded PHP throws me off a little. But I think what you want to do:
is initialize a counter to 0 before the while loop ($i = 0). Increment it at the end of the loop ($i++)
I'm not sure where you want to put these classes, but put if ($i%4==1) class="alpha" and if(i%4==0) class="omega" (add php tags as appropriate, for some reason they werent showing up right for me).

Categories