$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Show child taxonomies (that have posts) of the current parent taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Show child taxonomies (that have posts) of the current parent taxonomy

matteradmin8PV0评论

For the life of me I can't see why this isn't working. 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.

For the life of me I can't see why this isn't working. 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.

Share Improve this question edited Jan 27, 2019 at 11:58 Pete asked Nov 26, 2018 at 10:58 PetePete 1,0582 gold badges14 silver badges40 bronze badges 7
  • You should use is_tax('p_scales') and not is_taxonomy().. – Sally CJ Commented Nov 26, 2018 at 13:13
  • That doesn't work – Pete Commented Nov 26, 2018 at 15:10
  • Are you sure the taxonomy p_scales is valid? And that the WordPress query var catp exists for the requested URL? Try var_dump( $cur_catp ) and see if it's a proper ID of a valid term. – Sally CJ Commented Nov 27, 2018 at 14:45
  • p_scales is definitely the custom taxonomy slug. I don't know what you mean by the query var catp? – Pete Commented Nov 27, 2018 at 15:15
  • 1 Well, you got this code: $cur_catp = get_query_var('catp'); in the question.. And did you try the var_dump() right after that code? – Sally CJ Commented Nov 27, 2018 at 15:24
 |  Show 2 more comments

1 Answer 1

Reset to default 1

Props to Sally for improving on this to show post count

$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)) . '">' . // wrapped
          $subcat->name . ' (' . $subcat->count . ')' . '</a></li>';
    }
}

UPDATE

To show the posts count:

As pointed in the comment, and based on the above code, you can use $subcat->count to display the number of posts in the specific term.

Therefore I replaced the $subcat->name . '</a></li>' with:

$subcat->name . ' (' . $subcat->count . ')' . '</a></li>'

which outputs something like: Term Name (1), Another Term Name (0), etc.

For the original code in question (using wp_list_categories()), here's how to fix it, and the fixed code:

  1. Replace the is_category() with is_tax( 'p_scales' ).

  2. Replace the get_query_var( 'cat' ) with get_queried_object_id().

    if (is_tax('p_scales'))
    {
        $cur_cat = get_queried_object_id();
        if ($cur_cat)
        {
            $new_cats = wp_list_categories('show_option_none=&echo=false&child_of=' . // wrapped
              $cur_cat . '&depth=1&title_li=&show_count=1&hide_empty=1&taxonomy=p_scales');
            echo '' . $new_cats . '';
        }
    }
    
Post a comment

comment list (0)

  1. No comments so far