Skip to content

Instantly share code, notes, and snippets.

@odil-io
Last active January 15, 2019 08:32
Show Gist options
  • Save odil-io/58d4096e97ad013dedd81d37eeddee77 to your computer and use it in GitHub Desktop.
Save odil-io/58d4096e97ad013dedd81d37eeddee77 to your computer and use it in GitHub Desktop.
WordPress: List taxonomies and seperator
<?php
// Get the tags.
$tags = get_the_tags();
// Count the total of tags.
$total_tags = count($tags);
// Set integer to 0.
// This is used to track at which item the loop is.
$i=0;
// Validate that there are more then 1 tag.
if($tags && ($total_tags > 1) ):
// Loop trough all tags.
foreach($tags as $tag):
// Increase the integer.
$i++;
// Echo the tag name.
echo $tag->name;
// If $i is less then total of tags, add comma seperator.
if($i < ($total_tags - 1) ):
echo ', ';
// If $i is equal to total of tags, and add a 'and' between the second to last item and last item.
// Replace this with a subsitude in your native language.
elseif($i < ($total_tags) ):
echo " and ";
endif;
endforeach;
// There is only 1 tag.
else:
if($tags):
foreach($tags as $tag):
echo $tag->name;
endforeach;
endif;
endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment