• Resolved Uriahs Victor

    (@uriahs-victor)


    I have been at this for hours and can’t figure out how to do this.

    Here is what I am trying to achieve:

    https://www.reed.co.uk/courses/

    As you can see it looks like a parent – child relationship between the categories with an icon prepended to the parent category.

    I basically want the parent category along with an icon prepended and list it’s children.

    I’ve been trying and deleting code for hours, i’v read through get_categories and get_terms but still not figuring out how to do this.

    For the icon that’s the least of my worries, I might just use a switch statement to assign an icon based on the category name (these categories are unlikely to be changed)

    $parent = get_terms( 'course');
      //  $parent = get_category_parents( $cat_parent, true, ' » ' );
    
      foreach( $parent as $cat_parent ){
    
        echo '<div class="courses-container" style="float: left; border: 1px solid;">';
       echo '<h2><a href="' . get_term_link( $cat_parent ) . '">'.$cat_parent->name.'</a></h2>';
    
        $term_id = $cat_parent->term_id;
        $taxonomy_name = 'course';
        $termchildren = get_term_children( $term_id, $taxonomy_name );
    
        echo '<ul class="course-child">';
        foreach ( $termchildren as $child ) {
          $term = get_term_by( 'id', $child, $taxonomy_name );
          echo '<li><a href="' . get_term_link( $child ) . '">' . $term->name . '</a></li>';
        }
        echo '</ul>';
    
        echo '</div>';
      }

    The children categories are working perfectly and listing. But I am having some issues when I assign a course to more than one category whether parent or child. The page is populating with every category I assign I assign that post to, I just want all the parent categories and children listed below them. I know I’m close but I can’t figure it out

Viewing 1 replies (of 1 total)
  • Thread Starter Uriahs Victor

    (@uriahs-victor)

    Figured it out:

    <?php
    
      $parent = get_terms( 'course', 'parent=0&hide_empty=0');
    
      foreach( $parent as $cat_parent ){
    
        echo '<div class="courses-container" style="float: left; border: 1px solid;">';
        echo '<h2><a href="' . get_term_link( $cat_parent ) . '">'.$cat_parent->name.'</a></h2>';
    
        $term_id = $cat_parent->term_id;
        $taxonomy_name = 'course';
        $termchildren = get_term_children( $term_id, $taxonomy_name );
    
        echo '<ul class="course-child">';
        foreach ( $termchildren as $child ) {
          $term = get_term_by( 'id', $child, $taxonomy_name );
          echo '<li><a href="' . get_term_link( $child ) . '">' . $term->name . '</a></li>';
        }
        echo '</ul>';
    
        echo '</div>';
      }
    
      ?>
Viewing 1 replies (of 1 total)
  • The topic ‘List All Custom Taxonomy With Child’ is closed to new replies.