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 | Show 2 more comments1 Answer
Reset to default 1Props 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:
Replace the
is_category()
withis_tax( 'p_scales' )
.Replace the
get_query_var( 'cat' )
withget_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 . ''; } }
is_tax('p_scales')
and notis_taxonomy()
.. – Sally CJ Commented Nov 26, 2018 at 13:13p_scales
is valid? And that the WordPress query varcatp
exists for the requested URL? Tryvar_dump( $cur_catp )
and see if it's a proper ID of a valid term. – Sally CJ Commented Nov 27, 2018 at 14:45p_scales
is definitely the custom taxonomy slug. I don't know what you mean by the query varcatp
? – Pete Commented Nov 27, 2018 at 15:15$cur_catp = get_query_var('catp');
in the question.. And did you try thevar_dump()
right after that code? – Sally CJ Commented Nov 27, 2018 at 15:24