Skip to content

Instantly share code, notes, and snippets.

@fabianofa
Created September 7, 2014 01:55
Show Gist options
  • Save fabianofa/1be0110317642b8a0b4a to your computer and use it in GitHub Desktop.
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.
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