• Resolved Pete

    (@perthmetro)


    This function lists all the child categories for the current parent category/archive. For the life of me I can’t see why this isn’t working for custom taxonomies. It works fine like this (normal categories).

    <?php
    if (is_category())
    {
    $cur_cat = get_query_var('cat');
        if ($cur_cat) 
        {
            $new_cats = wp_list_categories('show_option_none=&echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&show_count=1&hide_empty=1');
            echo '' . $new_cats . '';
        }
    }
    ?>

    But it doesn’t work like this with a custom taxonomy…

    <?php
    if (is_taxonomy())
    {
    $cur_catp = get_query_var('catp');
        if ($cur_catp) 
        {
            $new_catsp = wp_list_categories('show_option_none=&echo=false&child_of=' . $cur_catp . '&depth=1&title_li=&show_count=1&hide_empty=1&taxonomy=p_scales');
            echo '' . $new_catsp . '';
        }
    }
    ?>

    I need both to work side by side hence why i’ve changed the variables.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to be supplying the term ID to the “child_of” argument. When you use get_query_var(), you are passing the term name. To get the ID of a term if you only have a name, use get_term_by().

    I’m surprised your category code works, the equivalent on my site only works with ID. For future stability, you should pass an ID here as well.

    I’d encourage you in general to use the array format of arguments instead of string anywhere that’s applicable. Arrays are more fully supported than strings, in that some functionality is only possible with arrays. Not so much for wp_list_categories(), but other functions. It takes a little more effort to type in an array argument, but the format is much easier to maintain going into the future.

    Thread Starter Pete

    (@perthmetro)

    Thanks, if I ask nicely would you like to update this code. I have very limited PHP skills i’m afraid.

    Thread Starter Pete

    (@perthmetro)

    I found and it work on archive.php without me having to specify the taxonomy…

        $term = get_queried_object();
    
        $children = get_terms( $term->taxonomy, array(
            'parent'    => $term->term_id,
            'hide_empty' => false
        ) );
    
        if ( $children ) { 
            foreach( $children as $subcat )
            {
                echo '<li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li>';
            }
        }
    Thread Starter Pete

    (@perthmetro)

    The only trouble i’m having with this is being able to show the number of posts next to each term? show_count doesn’t work.

    Moderator bcworkz

    (@bcworkz)

    You need to code it yourself in the echo statement. The number of posts is in $subcat->count

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Apply a custom taxonomy to wp_list_categories’ is closed to new replies.