Home › Forums › Add-ons › Repeater Field › Splitting Repeater Fields
So, I need to be able to split four repeater fields into separate columns for a layout. Essentially, I need to put four repeater fields into two separate divs, with two in each div, like so.
<div>
<div class="repeater-field-1"></div>
<div class="repeater-field-2"></div>
</div>
<div>
<div class="repeater-field-3"></div>
<div class="repeater-field-4"></div>
<div>
Any way that I can do this?
<div>
<?php
$count = 0;
while (have_rows('repeater_field')) {
the_row();
if ($count > 0 && ($count % 2 == 0)) {
// split every 2 rows
?>
</div>
<div>
<?php
}
?>
<div class="repeater-field-<?php echo $count+1; ?>"></div>
<?php
$count++;
}
?>
</div>
This worked for the most part. The only thing that’s happening is that it is showing three items in one div and two in the other. Here is the code I’m using.
<div id="features" class="features">
<div class="column-left">
<?php $count = 0; while (have_rows('features')) { the_row(); if ($count > 0 && ($count % 2 == 0))
{?>
<div class="item">
<h6><?php the_sub_field('title'); ?></h6>
<p><?php the_sub_field('description'); ?></p>
</div>
</div>
<div class="column-right">
<?php } ?>
<div class="item">
<h6><?php the_sub_field('title'); ?></h6>
<p><?php the_sub_field('description'); ?></p>
</div>
<?php $count++; } ?>
</div>
</div>
Any thoughts on this?
Do 1 of 2 things
1) Start count at 1 instead of 0
2) Increment $count at the beginning of the while loop instead of the end
Hmmm. I modified it like so, but it’s still showing the third item twice, once in the first div and once in the second.
<div class="column-left">
<?php $count = 1; while (have_rows('features')) { the_row(); if ($count > 1 && ($count % 2 == 1)) {?>
<div class="item">
<h6><?php the_sub_field('title'); ?></h6>
<p><?php the_sub_field('description'); ?></p>
</div>
</div>
<div class="column-right">
<?php } ?>
<div class="item">
<h6><?php the_sub_field('title'); ?></h6>
<p><?php the_sub_field('description'); ?></p>
</div>
<?php $count++; } ?>
</div>
</div>
Looked at the code closer, the problem is where you’re putting the if. Basically, when the count is right you just want to close a div and open the next one.
<div class="column-left">
<?php
$count = 0;
while (have_rows('features')) {
the_row();
if ($count > 0 && ($count % 2 == 0)) {
?>
</div>
<div class="column-right">
<?php
}
?>
<div class="item">
<h6><?php the_sub_field('title'); ?></h6>
<p><?php the_sub_field('description'); ?></p>
</div>
<?php
$count++;
}
?>
</div>
Someone Please check I have the same problem and I can’t fix it.
I want to take the different values of sub_field “start_date” and display posts with individual value.
My code in case it helps `<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
* Learn more: http://codex.wordpress.org/Template_Hierarchy
*
* @package presscore
* @since presscore 0.1
*/
// File Security Check
if ( ! defined( ‘ABSPATH’ ) ) { exit; }
$config = Presscore_Config::get_instance();
$config->set( ‘template’, ‘blog’ );
$config->set( ‘layout’, ‘list’ );
$config->set( ‘template.layout.type’, ‘list’ );
$config->set( ‘post.preview.media.width’, 30 );
get_header(); ?>
<div id=”content” class=”content” role=”main”>
<?php if ( have_posts() ) : ?>
<div class=”articles-list”>
<?php do_action( ‘presscore_before_loop’ ); ?>
<?php update_post_thumbnail_cache(); ?>
<?php
$count = 0;
$your_repeater = get_field(‘sub_seminars’);
if($your_repeater){
while( have_rows(‘sub_seminars’) ): the_row();
$count++;
$my_field = get_sub_field(‘start_date’);
if ($count == 1) { ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// populate config with current post settings
presscore_populate_post_config();
presscore_get_template_part( ‘theme’, ‘blog/list/blog-list-post’ );
?>
<?php endwhile; ?>
<?php } endwhile; } ?>
<?php do_action( ‘presscore_after_loop’ ); ?>
</div>
<?php dt_paginator(); ?>
<?php else : ?>
<?php get_template_part( ‘no-results’, ‘blog’ ); ?>
<?php endif; ?>
</div>
<?php get_sidebar(‘primary’); ?>
<?php get_footer(); ?>`
The topic ‘Splitting Repeater Fields’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.