How to get children items of specified taxonomy term inside a shortcode

Question

The following shortcode list children items of a custom term type, how can I add the functionality to list the children of a specified term instead of listing the children on all terms. e,g

[categories_shortcode tax-slug="drinks"]

Here is the PHP code for the shortcode:

add_shortcode( 'categories_shortcode', 'categories_list' );
function categories_list() {
ob_start();
$taxonomyName = "categories";
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) ); ?>
<ul>
   <?php
      foreach ( $parent_terms as $pterm ) {
          $terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
          foreach ( $terms as $term ) : ?>
   <li><a href="<?php get_term_link( $term ) ?>"><?php echo $term->name ?></a></li>
   <?php endforeach; 
      }
      ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php $categories_list = ob_get_clean();
return $categories_list;
}
0
Ahmad Tahhan 3 months 0 Answers 9 views 0

Leave an answer