• I use the following code to display sub categories with category pages on my site, I have categories set up as counties of the UK…

    <?php if (is_category()) {
      $this_category = get_category($cat);
      if (get_category_children($this_category->cat_ID) != "") {
    echo "<div class=\"countysubs\"><h2>Counties</h2>";
        echo "<ul>";
        wp_list_categories('orderby=id&show_count=0&title_li=
    &use_desc_for_title=1&child_of='.$this_category->cat_ID);
        echo "</ul></div>";
    
      }
    } ?>

    However, I now want to use a hierarchical custom taxonomy to categorise posts by county, and so I am wondering how I can change the above code so that it can be used for a custom taxonomy.

    I have searched everywhere and cannot find a solution, can anone here help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter WebFlair

    (@webflair)

    Anybody able to help with this?

    I have not tested this but I think this should work using get_term_children function.

    $taxonomyName = 'taxonomy_name'
    $terms = get_term_children($post->ID, $taxonomyName);
    echo '<ul>';
    foreach ($terms as $term) {
    	$term = get_term_by( 'id', $child, $taxonomyName );
    	echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';

    If its not working it might be to do with the $post->id which you could get the post id by using the get_queried_object_id in WP Query.

    $id = $wp_query->get_queried_object_id();
    $terms = get_term_children($id->term_id, $taxonomyName);
    Thread Starter WebFlair

    (@webflair)

    That seems to produce a 500 server error, perhaps I am using it incorrectly.

    Thread Starter WebFlair

    (@webflair)

    Here’s a better description of my problem…

    On my wordpress site UK Holiday Parks I use categories as locations i.e. Regions and Counties

    I have the top level categories (regions) in the side bar and the sub categories on the archive.php template using the code below…

    <?php if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
    echo "<div class=\"countysubs\"><h2>Counties</h2>";
    echo "
    <ul>";
    wp_list_categories('orderby=id&show_count=0&title_li=
    &use_desc_for_title=1&child_of='.$this_category->cat_ID);
    echo "</ul>
    </div>";
    
    }
    } ?>

    I want to stop using categories for locations and use a hierarchical custom taxonomy instead.

    I have set up a the Taxonomy, and a know how to get top level categories for the custom taxonomy into the side bar, I just cannot seem to get sub categories from the custom taxonomy to show up in the archive.php template.

    Hi Friends,

    I am new to wordpress plugin development. I want to create my plugin for directory listings which consists of categories and sub categories.

    Can u please give me any link for the tutorial that how to use my custom categories and posts.

    I shall be very thankfull to you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Sub Categories in custom taxonomies’ is closed to new replies.