Skip to content

Instantly share code, notes, and snippets.

@Alipio123
Created October 20, 2019 14:36
Show Gist options
  • Save Alipio123/2f7e4cb6d555ac34fac47b89961411cc to your computer and use it in GitHub Desktop.
Save Alipio123/2f7e4cb6d555ac34fac47b89961411cc to your computer and use it in GitHub Desktop.
Getting parent category and showing it through wordpress shortcode paste this code in functions.php
/**
* Create [parent_category] shortcode
* Shortcode can have this attribute
* taxonomy //string
*/
add_shortcode( 'parent_category', 'get_parent_category' );
function get_parent_category( $atts ) {
$arg = shortcode_atts( array(
'taxonomy' => 'category', //default taxonomy
), $atts );
$post_cats = get_the_terms( get_the_ID(), $arg['taxonomy'] );
$cats_list = array();
foreach( $post_cats as $post_cat ) {
if( $post_cat->parent == 0 ) {
$cats_list[] = $post_cat->name;
}
}
$cats_html = join( ", ", $cats_list );
return $cats_html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment