Skip to content
Advertisement

WordPress taxonomy last child separated by “en” instead of comma

I’m trying to create a shortcode that outputs my custom taxonomies, separated by a comma, but i want the last comma to be “en” instead of a comma. So like this:

taxonomy, taxonomy, taxonomy en taxonomy

So far i have this:

JavaScript

Advertisement

Answer

As I dont have an example of the $terms or $entry_terms variable I had to make up some dummy data, but I think you should be able to extract my example and place it into your code.

I made use of the ternary operator (https://www.php.net/manual/en/language.operators.comparison.php) to determine whether or not the final comma should be ‘,’ or ‘en’:

JavaScript

This outputs:

JavaScript

This should work with any array length, e.g. if $terms only has 2 elements:

JavaScript

Or 1 element:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement