Advertisement
rexcoder

Get Single child category of a Parent

Mar 31st, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.91 KB | None | 0 0
  1. if ( ! function_exists( 'themeplate_get_child_category' ) ) {
  2.     function themeplate_get_child_category( $id ) {
  3.         $taxonomy = 'category';
  4.         global $post;
  5.  
  6.         // Get the term IDs assigned to post.
  7.         $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
  8.  
  9.         // Separator between links.
  10.         $separator = ', ';
  11.  
  12.         if ( ! empty( $post_terms ) && ! is_wp_error( $post_terms ) ) {
  13.  
  14.             $term_ids = implode( ',' , $post_terms );
  15.  
  16.             $terms = wp_list_categories( array(
  17.                 'title_li' => '',
  18.                 'child_of'  => $id,
  19.                 'style'    => 'none',
  20.                 'echo'     => false,
  21.                 'taxonomy' => $taxonomy,
  22.                 'include'  => $term_ids
  23.             ) );
  24.  
  25.             $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );
  26.  
  27.             // Display post categories.
  28.             return '<div class="child-cat">' . $terms . '</div>';
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement