0

How can i show the_terms with commas and "&" before the last one

Example:

Tags: books, review & history

I need show a "," between the terms and a "&" before the last term.

I'm using the following code to show the_terms in the single post:

<?php the_terms( $post->ID, 'post_tag', 'Tags: ', ', ', ' ' ); ?>
1

1 Answer 1

0
$terms = get_the_term_list( $post->ID, 'post_tag', 'Tags: ', ', ', '');
echo preg_replace('/^(.+),([^,]+)$/', '$1 &$2', $terms);

Explanation for /^(.+),([^,]+)$/:

/     - delimiter
^     - string beginning
.+    - 1 or more characters
[^,]+ - 1 or more characters that are not a comma
$     - string end

$n is the nth match i.e. nth pair of brackets

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.