Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rosschapman/5904637 to your computer and use it in GitHub Desktop.
Save rosschapman/5904637 to your computer and use it in GitHub Desktop.
wp_get_category_names_for_any_post.php #wordpress
<?php
function get_category_names_for_any_post(){
// Create empty array for later
$categoriesArray = array();
// Get current post type
$curentPostType = get_post_type($post->ID);
// Get taxonomy for current post
$curentPostTypeTaxonomyObject = get_object_taxonomies( $curentPostType );
// Get taxonomy terms for current post
$currentPostTerms = wp_get_post_terms( $post->ID, $curentPostTypeTaxonomyObject);
// Loop through terms and create new array of term names
foreach ($currentPostTerms as $term ) {
$categoriesArray[] = $term->name;
}
return $categoriesArray;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment