Created
October 20, 2019 14:36
-
-
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
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
/** | |
* 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