Created
September 7, 2014 01:55
-
-
Save fabianofa/1be0110317642b8a0b4a to your computer and use it in GitHub Desktop.
Return either an array with all categories name from a post or a formatted string that bay be echoed.
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
function get_categories_name($post_id, $formated = false, $separator = " + "){ | |
$categories = wp_get_post_categories($post_id); | |
$categories_name = array(); | |
$formatted_string = ""; | |
foreach ($categories as $category){ | |
array_push($categories_name, get_category($category)->name); | |
} | |
if (!$formated) | |
return $categories_name; | |
foreach ($categories_name as $key => $name){ | |
$formatted_string .= $name; | |
if ($key < count($categories_name) - 1) | |
$formatted_string .= $separator; | |
} | |
return $formatted_string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment