0
votes

Before I start, I'm asking for your help because I have very basic html and css skills, and absolutely no php knowledge !

I wanted to display the subcategories on category.php template, below the caterogy title and description. I found a code on stackoverflow which works perfectly (to which I added a div class) :

<div class="sub-cat-inline"><?php
    // if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
    if ($cur_cat) 
    {
        $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&hide_empty=0');
        echo '<ul>' . $new_cats . '</ul>';
    }
}
?>
</div>

However, when charging a page of a category or subcategory not containing any subcategory, there is this text being displayed : "No category".

In the php code above, is it possible to hide any text when there is no subcategory ? Thanks a lot

If anyone interested, below is the basic css used to style the div :

.sub-cat-inline li {
    width: 100%;
    display: inline;
    padding: 10px;

}

.sub-cat-inline {
    text-align: center;
}
3

3 Answers

0
votes

You can just use it like this:

<div class="sub-cat-inline"><?php
    // if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
    if ($cur_cat) 
    {
        $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&hide_empty=0'); 
        if(!empty($new_cats)){
            echo '<ul>' . $new_cats . '</ul>';
        }
    }
}
?>
</div>
0
votes

You can use the hide_if_empty parameter to exclude from the list those categories that don't have posts attached to them:

<?php

<div class="sub-cat-inline"><?php
    // if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
    if ($cur_cat) 
    {
        $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&hide_empty=1');
        echo '<ul>' . $new_cats . '</ul>';
    }
}
?>
</div>

Also, if no categories are found the wp_list_categories() function will return a 'No categories' text (as you already noticed).

To change what the function returns when no categories are found, you can use the show_option_none parameter:

<?php

<div class="sub-cat-inline"><?php
    // if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
    if ($cur_cat) 
    {
        $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&hide_empty=1&show_option_none=&nbsp;');
        echo '<ul>' . $new_cats . '</ul>';
    }
}
?>
</div>
0
votes

There is a workaround using css, as the "No category" text is linked to a css property by default : cat-item-none

Using display: none; hides that text, but I'm sure there is a way to prevent that text from being displayed with the php code, right ?

I tried changing the value hide_empty=0 to hide_empty=1 but it doesn't do anything... Any idea ?

<div class="sub-cat-inline"><?php
    // if the page visitor views is a category page
if (is_category())
{
$cur_cat = get_query_var('cat');
    if ($cur_cat) 
    {
        $new_cats = wp_list_categories('echo=false&child_of=' . $cur_cat . '&depth=1&title_li=&hide_empty=0');
        echo '<ul>' . $new_cats . '</ul>';
    }
}
?>
</div>