Last active
August 21, 2016 11:27
-
-
Save mwangepatrick/e956d11504c4c01d1524 to your computer and use it in GitHub Desktop.
This is a sample code on how to sort repeater fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<head> | |
<?php | |
wp_head(); | |
?> | |
</head> | |
<body> | |
<?php | |
/* | |
* get repeater value | |
*/ | |
$repeater = get_field('performance'); | |
/* | |
* Obtain a list of columns | |
* - $column_id will look something like this: | |
Array | |
( | |
[0] => 3 | |
[1] => 2 | |
[2] => 4 | |
[3] => 1 | |
) | |
*/ | |
foreach ($repeater as $key => $row) { | |
$column_id[$key] = $row[4]; //select the of the item you want to use to short ie(date) | |
} | |
/* | |
* Use the $column_id array to sort the $repeater array | |
*/ | |
array_multisort($column_id, SORT_ASC, $repeater); | |
/* | |
if your array is not big you could also use usort | |
//usort($repeater, function ($a, $b) { return strcmp($a[4], $b[4]);); | |
*/ | |
/* | |
* Do stuff with the repeater... Note: has_sub_field loop will not use the "sorted" $repeater array | |
*/ | |
?> | |
<ul class="list-unstyled listings"> | |
<?php | |
$i = 0; | |
foreach ($repeater as $row) { | |
//replace your code in here | |
$i++; | |
if ($i >= 2): //replace this with your limit of rows to display | |
?> | |
<li> | |
<div class="row"> | |
<div class="col-sm-3 col-md-3 article-logos"><img src="<?php | |
the_sub_field('pic'); | |
?>" class="img-responsive"></div> | |
<div class="col-sm-9 col-md-9"> | |
<p class="meta small"> | |
<?php | |
the_sub_field('my_date'); | |
?> | |
</p> | |
<p><a href="<?php | |
the_sub_field('my_link'); | |
?>"> | |
<?php | |
the_sub_field('year'); | |
?> | |
</a></p> | |
</div> | |
</div> | |
</li> | |
<?php | |
endif; | |
} | |
?> | |
</ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment