0

I got an issue.

I got a list of artists as an attribute.

I made this code to show it on a page:

$terms = get_terms( 'pa_artists' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     echo '<ul class="artistes">';
     foreach ( $terms as $term ) {
       echo '<li><a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a></li>';

     }
     echo '</ul>';
 }

So far so good…

But i created the attributes like this:

Pablo Picasso
Paul Gauguin
Auguste Renoir

But i need to reverse the order of the words… To show it like this: (And if possible the first word in bold)

PICASSO Pablo
GAUGUIN Paul
RENOIR Auguste

I tried to make an explode string to do it, but couldn't make it work.

Any idea?

  • The answer to https://stackoverflow.com/questions/2977556/how-to-reverse-words-in-a-string#2977638 applied to `$term->name` appears to be the solution here – Tom J Nowell Sep 13 '18 at 01:10
  • Names are difficult things. In your examples you only have one firstname and one surname, but what about Michelangelo or Vincent van Gogh or Peter Paul Rubens or even Pieter Bruegel the Elder or J.M.W. Turner? – Gordon Sep 13 '18 at 15:07

2 Answers2

0

i think you have array, before foreach loop you can use array_flip,

Try this.

$terms = get_terms( 'pa_artists' );
 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
     echo '<ul class="artistes">';
      $terms = array_flip($terms);

     foreach ( $terms as $term ) {
       echo '<li><a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a></li>';

     }
     echo '</ul>';
 }
jvk
  • 2,133
  • 3
  • 19
  • 28
0

Try the following code and integrate that in your loop:

$string = "Pablo Picasso";

$array = explode(" ", $string);

$array[1] = strtoupper($array[1]);

$array[1] = "<strong>".$array[1]."</strong>";

$rarray = array_reverse($array);

echo $newstring = implode(" ", $rarray);

Output is: PICASSO Pablo

unixmiah
  • 3,081
  • 1
  • 12
  • 26