Home › Forums › Add-ons › Repeater Field › Difficulty sorting on repeater sub-field
I’ve looked through this page and *sort of* have things working, but just not quite.
It’s listing all objects, but only shows one <h2> for all of them. What am I missing? I need one grouping of <h2></h2>
for each sub_field titled ‘position’.
So, it would look something like:
<h2>Position 1</h2>
<h2>Position 2</h2>
This is my code thus far:
<?php // get repeater field data
$repeater = get_field('house_forms');
// vars
$order = array();
// populate order
foreach( $repeater as $i => $row ) {
$order[ $i ] = $row['position'];
}
// multisort
array_multisort( $order, SORT_DESC, $repeater );
// loop through repeater
if( $repeater ): ?>
<h2><?php echo $row['position']; ?></h2>
<ul class="forms">
<?php foreach( $repeater as $i => $row ): ?>
<li class="form"><?php echo $row['form_name']; ?>
<a href="<?php echo $row['document']; ?>" target="_blank">Download</a><?php if( $row['online'] ): ?> | <a href="<?php echo $row['online']; ?>">Fill out online</a><?php endif; ?>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
That’s because you put the code outside of the loop. It should be something like this:
// loop through repeater
if( $repeater ):
// initialize the position for later use
$position = '';
?>
<ul class="forms">
<?php foreach( $repeater as $i => $row ): ?>
<?php
// check if the current postion is changed
if( $position != $row['position'] ){
// set the current position
$position = $row['position'];
?>
<h2><?php echo $row['position']; ?></h2>
<?php } ?>
<li class="form"><?php echo $row['form_name']; ?>
<a href="<?php echo $row['document']; ?>" target="_blank">Download</a><?php if( $row['online'] ): ?> | <a href="<?php echo $row['online']; ?>">Fill out online</a><?php endif; ?>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); ?>
Because this is more related to PHP, kindly visit their community for further support.
I hope this helps 🙂
Ahhhhh…I see. Thank you. And I’m sorry I didn’t realize it was a purely PHP problem. I appreciate the help.
The topic ‘Difficulty sorting on repeater sub-field’ 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.