如何让Get_Categories()的‘Hide_Empty’参数与存档模板中的全局年份变量一起使用?

时间:2016-09-02 作者:Peter Westerlund

当我使用get_categories() 在存档中。php文件,已激活hide\\u empty(true | 1),然后向该文件发送年份值。hide\\u empty参数失去了作用。无论如何,所选年份中没有帖子的类别都会显示出来。

如果没有从所选年份连接的帖子,如何隐藏类别?

Edit: 我的代码:

<?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
}
?>

1 个回复
SO网友:Turi S.

为什么不在打印之前添加一条if语句来检查$count是否大于0(<;li>;第13行:

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