-
-
Save tommcfarlin/cac5873df8d5e4ff0a37e7ab81972cfb to your computer and use it in GitHub Desktop.
[WordPress] Writing Loops in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, '|'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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