Home › Forums › Add-ons › Repeater Field › Repeater fields reverse order
Hi everybody, i’m in nowhere now with repeater fields. My situation is;
I have a post type which is “dizi” and my client want to add new epsodie for each serie. I have created a repeater name which name is dizi_sezon_1 and they add each epsodie easyly. Its ok.
But when i want to show last one epsoide from each serie’s on home page. I try break and reverse oder code. Its make good result but now show only first epsodie not last.
For example High School DxD New Special has 6 epsodie but it show only the first one 1.bölüm
My code is here
<?php
$args = array(
'post_type' => array( 'dizi' ) ,
'posts_per_page' => 12,
'orderby' => 'date',
'order' => 'DESC',
);
$angebot_3 = new WP_Query( $args );
while( $angebot_3->have_posts() ) : $angebot_3->the_post() ;
?>
<?php $outs = array(); if( have_rows('dizi_sezon_1') ):
$i = 0;
while ( have_rows('dizi_sezon_1') ) : $i++; the_row();
if( $i > 1 )
{
break;
}
ob_start();
?>
<div class="col-md-6 col-sm-12 col-xs-12 icon-renkleri">
<div class="col-md-3 no-padding">
<a href="<?php the_permalink();?>" title="<?php the_title();?>">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('thumb-filmdetay'); } ?>
</a>
</div>
<div class="col-md-9">
<div class="clearfix"></div>
<h4 class="film-basligi-rengi no-margin "><a href="<?php the_permalink(); ?>" title="<?php the_title();?>"><?php the_title();?> - <?php the_sub_field('bolum_adi'); ?></a>
</h4>
<div class="clearfix"></div>
<div class="row">
<div class="clearfix"></div>
<?php if( get_field('cevirmeni') ): ?>
<div class="col-md-12 col-sm-12 col-xs-6 margin-top-10 "><i class="fa fa-user f15 "></i> :
<?php $terms = wp_get_post_terms($post->ID, 'cevirmen'); ?>
<?php $malzeme = get_field('cevirmeni'); ?>
<?php foreach ($terms as $term): ?>
<a class="renk-gri" href="<?php echo get_term_link( $term ); ?>"> <?php echo $term->name; ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if( get_field('encoderi') ): ?>
<div class="col-md-12 col-sm-12 col-xs-6 margin-top-10 "> <i class="fa fa-star f15 "></i> :
<?php $encoderi = get_field('encoderi'); ?>
<a class="renk-gri " href="<?php echo get_term_link( $encoderi ); ?>"> <?php echo $encoderi->name; ?></a> </div>
<?php endif; ?>
<div class="clearfix"></div>
</div>
</div>
</div>
<?php
$outs[] = ob_get_clean(); endwhile;
else :
endif;
$outs = array_reverse($outs);
echo implode($outs);
?>
<?php endwhile; ?>
Where i’m wrong
What you need to do is know how many rows are in the repeater and loop through it without doing anything until you get to the last one.
if (have_rows('dizi_sezon_1')) {
// use get_post_meta
// it will return the number of rows in the repeater
$row_count = intval(get_post_meta($post->ID, 'dizi_sezon_1', true));
$i = 0;
while (have_rows('dizi_sezon_1')) {
$i++;
if ($i<$row_count) {
continue;
}
// will only get here on the last row
// put code here to display row
}
}
Hi @alicinaroglu,
Thanks for the mail.
Please read over the following article:
http://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/
This should help understand how to sort the repeater field array.
I hope this helps.
Hi again, thanks for your reply but i think there is a misunderstanding.
I want to show last row as a post.
But my code show first row.
Its my problem
Try this!
<div class="main-div">
<?php $outs = array(); if( have_rows('main_field') ): $i = 0;
while ( have_rows('main_field') ) : the_row(); ob_start(); $i++; ?>
<div class="question-answer-template">
<?php the_sub_field('sub_feild');?>
</div>
<?php $outs[] = ob_get_clean(); endwhile;
else :
endif;
$outs = array_reverse($outs);
echo implode($outs);
?>
</div>
The topic ‘Repeater fields reverse order’ 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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.