Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created January 10, 2019 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/cac5873df8d5e4ff0a37e7ab81972cfb to your computer and use it in GitHub Desktop.
Save tommcfarlin/cac5873df8d5e4ff0a37e7ab81972cfb to your computer and use it in GitHub Desktop.
[WordPress] Writing Loops in PHP
<?php
// Setup the array to store the tag names.
$tagNames = [];
// Iterate through all of the tags.
$tags = get_the_tags();
for ($i = 0; $i < count($tags); $i++) {
$currentTag = $tags[$i];
// Only add names that have not been added.
if (!in_array($currentTag->name, $tagNames)) {
$tagNames[] = $currentTag->name;
}
}
// Break the names of the the tags into a '|' delimited string.
$tagNameString = implode('|', $tagNames);
// Clean up the string so any trailing pipes are removed.
$tagNameString = rtrim($tagNameString, '|');
<?php
// Seperate each indidivudal entry by a '|'
implode(
'|',
// Remove any duplicates in the array.
array_filter(
/* iterate through the list of tags and add the names to an
* array that's returned to filter.
*/
array_map(function ($tag) {
return $tag->name;
}, get_the_tags())
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment