Skip to content

Instantly share code, notes, and snippets.

@odil-io
Last active September 4, 2017 14:24
Show Gist options
  • Save odil-io/596b408874ee8410db236edcdbcf1b0b to your computer and use it in GitHub Desktop.
Save odil-io/596b408874ee8410db236edcdbcf1b0b to your computer and use it in GitHub Desktop.
WordPress: comma seperated get_the_tags() list with "and"
<?php
// Get the tags
$tags = get_the_tags();
// Count total tags
$total_tags = count($tags);
// Set $i
$i=0;
// Continue when we have more than 1 tags
if($tags && ($total_tags > 1) ):
foreach($tags as $tag):
$i++;
// echo tag
echo $tag->name;
// If less then total tags minus one so we seperate all tags except the second last and last.
if($i < ($total_tags - 1) ):
echo ', ';
// If second last tag seperate with "and"
elseif($i < ($total_tags) ):
echo " and ";
endif;
endforeach;
// Else, if we only have one 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