Skip to content

Instantly share code, notes, and snippets.

@evanfraser
Created January 2, 2020 22:32
Show Gist options
  • Save evanfraser/5df9a260805a0d3208f89d07c6b22b09 to your computer and use it in GitHub Desktop.
Save evanfraser/5df9a260805a0d3208f89d07c6b22b09 to your computer and use it in GitHub Desktop.
<?php
/**
* Get Post Category
*
* Shortcode: [post_category]
*/
function shortcode_post_category($atts=array()) {
// get taxonomy by post type
$tax = get_object_taxonomies(get_post_type());
// get the categories of the taxonomy
$cats = get_the_terms($post->ID, $tax[0]);
$result = '';
foreach ($cats as $cat) {
$result = $cat->name;
return $result;
}
return $result;
}
add_shortcode('post_category', 'shortcode_post_category');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment