0

When I use get_categories() in the archive.php file, having hide_empty activated (true|1), and then sending a year value to the file. The hide_empty argument lost it's effect. Categories that doesn't have posts from the selected year shows anyway.

How do I get the categories to be hidden if it doesn't has posts connected from the selected year?

Edit: My code:

<?php
if (!empty($_GET['m']))
{
?>
<h2><?php echo _e('Categories'); ?></h2>
<ul>
    <?php
    foreach(get_categories('parent=0&hide_empty=1') as $category)
    {
        $my_query = new WP_Query('cat='.$category->term_id.'&year='.$_GET['m'].'&posts_per_page=-1');
        $count = $my_query->post_count;
    ?>
    <li><a class="name" href="?cat=<?=$category->term_id?>&year=<?=$_GET['m']?>"><?=$category->name?> (<?=$count?>)</a></li>

    <?php
    $cats = get_categories('parent='.$category->term_id.'&year='.$_GET['m'].'&hide_empty=1');

    if (count($cats))
    {
    ?>
    <ul>
    <?php
    foreach($cats as $category)
    {
        $my_query = new WP_Query('cat='.$category->term_id.'&year='.$_GET['m'].'&posts_per_page=-1');
        $count = $my_query->post_count;
    ?>
    <li><a class="name" href="?cat=<?=$category->term_id?>&year=<?=$_GET['m']?>"><?=$category->name?> (<?=$count?>)</a></li>
    <?php
    }
    ?>
    </ul>
    <?php
    }
    }
    ?>
</ul>
<?php
}
?>
0

Why not add an if statement to check whether $count > 0 before printing your <li> on line 13:

<?php if($count > 0): ?>
    <li><a class="name" href="?cat=<?=$category->term_id?>&year=<?=$_GET['m']?>"><?=$category->name?> (<?=$count?>)</a></li>
<?php endif; ?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.