0

We have these PHP functions

$taxcomposer = strtoupper(get_the_term_list( get_the_ID(), 'composer', '', ', '  )) ;
$taxarr = strtoupper(get_the_term_list( get_the_ID(), 'arranger', '', ', '  ));
$taxlyrics = strtoupper(get_the_term_list( get_the_ID(), 'lyrics', '', ', '  ));

To display it we use this code

$frontpage = '
<p style="padding-top: 9pt;text-align: center;">by</p>
<p style="padding-top: 1pt;text-align: center;">'.$taxcomposer.''.$taxarr.''. $taxlyrics.' </p>';

The front end result:
By
MARK W. WINGSTON, FILIP STONEEVA NYMAN, JOHN SCOTTFRIDA M. HAMELTON, HENRIK GRIEG

I want it to look like this:
By
MARK W. WINGSTON, FILIP STONE, EVA NYMAN, JOHN SCOTT, FRIDA M. HAMELTON and HENRIK GRIEG


I guess we need to array it somehow and then replace the final comma with “and”, how do we achieve this?

Update

    $taxcomposer = get_the_terms( get_the_ID(),'composer');
    $taxarr = get_the_terms( get_the_ID(),'arranger');
    $taxlyrics = get_the_terms( get_the_ID(),'lyrics');
    
    $terms = array_merge( $taxcomposer, $taxarr, $taxlyrics);
    $links = array();
    foreach ($terms as $term) {
    $link = get_term_link( $term );
    $links[] =  '<a style="text-transform: uppercase;" href="'.esc_url( $link ).'">'.$term->name.'</a>';         
}
    $last = array_pop( $links );
    $string = count($links) ? implode(", ", $links) . " & " . $last : $last;    

Error log

array_merge(): Expected parameter 2 to be an array, bool given Warning: Invalid argument supplied for foreach()

If the taxonomy is empty then it results in not displaying anything…

Solution
Not found yet…

  • Does this answer your question? [Replace last occurrence of a string in a string](https://stackoverflow.com/questions/3835636/replace-last-occurrence-of-a-string-in-a-string) – CBroe Jun 30 '22 at 07:33
  • `get_the_terms` returns `false`, if no terms were found. You can not feed false to `array_merge`, that will only result in a warning or fatal error (depending on PHP version.) – CBroe Jun 30 '22 at 07:57
  • Right, `??` only works for `null` values. `$taxcomposer = get_the_terms(...); $taxcomposer = $taxcomposer ? $taxcomposer : [];` – CBroe Jun 30 '22 at 08:00
  • Can you please show me through JSFiddle? https://jsfiddle.net/cxa1dop9/ – Pontus Eriksson Jun 30 '22 at 08:15

0 Answers0