Reputation: 13
I'm trying to sort names in alphabetical order after a reverse array.
It's a code done for ordering last name/first name in right order. A few bugs, (like with names with middle names) but it works except the sorting.
Here is the code:
<?php
$terms = get_terms( 'pa_artist' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul class="artists">';
foreach ( $terms as $term ) {
$array = explode(" ", $term->name);
if ($array[3]) {
$array[3] = strtoupper($array[3]);
$array[3] = "<strong>".$array[3]."</strong>";
}
elseif ($array[2]) {
$array[2] = strtoupper($array[2]);
$array[2] = "<strong>".$array[2]."</strong>";
} elseif ($array[1]) {
$array[1] = strtoupper($array[1]);
$array[1] = "<strong>".$array[1]."</strong>";
} else {
$array[0] = strtoupper($array[0]);
$array[0] = "<strong>".$array[0]."</strong>";
}
$rarray = array_reverse($array);
sort($rarray);
echo '<li><a href="' .get_term_link( $term ). '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . implode(" ", $rarray) . '</a></li>';
}
echo '</ul>';
}
For now the names are ordered as if the reverse was never done.
Some examples, at first it showed like this:
Auguste Renoir
Pablo Picasso
Paul Gauguin
After the reverse and If strings, it's like this:
RENOIR Auguste
PICASSO Pablo
GAUGUIN Paul
When i need it:
GAUGUIN Paul
PICASSO Pablo
RENOIR Auguste
I tried every sort fonction, can't make it work… I can't find a way to sort after a reverse array, is it even possible?
It's for a list of names builded with attributes on wordpress/woocommerce.
I already asked that question, got answers that didn't work unfortunately…
There is Something like 150 names that needs ordering.
I'm willing to pay for this, but no-one is interested because it doesn't require much time so won't pay much! (Only got request to redo the whole website…)
Upvotes: 1
Views: 392
Reputation: 253814
Try the following:
$terms = get_terms( [ 'taxonomy' => 'pa_artist', 'hide_empty' => false ] );
$names_html = $terms_data = [];
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
// 1st Loop: Loop through the terms
foreach ( $terms as $term ) {
$fragments = explode( ' ', $term->name );
if( sizeof($fragments) > 1 ) {
// Manipulate and format the term name
$fragment = '<strong>' . strtoupper($fragments[1]) .'</strong>';
$new_term = $fragment . ' ' . $fragments[0];
// 1st array: We set each formatted term name
$names_html[] = $new_term;
// 2nd array: We set the related data as the Url and the original term name
$terms_data[$new_term] = array(
'link' => get_term_link( $term ),
'name' => $term->name,
);
}
}
// Sort the formatted term names
sort($names_html);
// Output
echo '<ul class="artists">';
// 2nd Loop: Loop through the sorted formatted term names
foreach ( $names_html as $name_html ) {
$link = $terms_data[$name_html]['link'];
$title = sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $terms_data[$name_html]['name'] );
echo '<li><a href="' . $link . '" title="' . $title . '">' . $name_html . '</a></li>';
}
echo '</ul>';
}
Tested and works.
Upvotes: 2