Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AlexWebLab/3cebb51505ca6a0bb40ffd255e2ff13f to your computer and use it in GitHub Desktop.
Save AlexWebLab/3cebb51505ca6a0bb40ffd255e2ff13f to your computer and use it in GitHub Desktop.
Order an associative array by a specific column.
<?php // On this example I'm extracting all the WordPress categories from a parent category and order them by an additional custom field named "order" assigned to the category taxonomy. ?>
<?php
$siblings = get_term_children($current_cat->term_id, 'category');
$siblings_list = array( array() );
$i=0;
foreach ($siblings as $sibling) {
$sibling_object = get_term_by( 'id', $sibling, 'category');
$siblings_list[$i]['name'] = $sibling_object->name;
$siblings_list[$i]['url'] = get_term_link( $sibling, 'category');
$siblings_list[$i]['order'] = get_field('order', $sibling_object->taxonomy.'_'.$sibling_object->term_id);
$siblings_list[$i]['image'] = get_field('image', $sibling_object->taxonomy.'_'.$sibling_object->term_id);
$i++;
}
// sort the array by the order
$order = array();
foreach ($siblings_list as $key => $row) {
$order[$key] = $row['order'];
}
array_multisort($order, SORT_ASC, $siblings_list); ?>
<?php for($k=0; $k<sizeof($siblings_list); $k++) { // echo the sorted array ?>
<a class="child_page_link <?php if($current_cat->name==$siblings_list[$k]['name']) echo 'current'; ?>" href="<?php echo $siblings_list[$k]['url']; ?>" title="<?php echo $siblings_list[$k]['name']; ?>" style="background-image:url('<?php echo $siblings_list[$k]['image']; ?>');"><?php echo $siblings_list[$k]['name']; ?></a>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment