0

我有一个返回的子类别列表。我只能访问模板显示而不能访问 MySQL 查询,所以不,我不能限制查询。

现在所有结果都返回了。我想将列表限制为 5 个,如果结果超过 5 个,则添加一个“更多”链接。

我知道我做错了,因为我认为count实际上与以下内容无关foreach

<ul class="sub-categories">
    <?php
    while (count($category->getChildren()) <= 5) { // line I added for while loop
        foreach ($category->getChildren() as $child) {
            if (!$child->totalItemCount())
                continue;

            $link = $this->app->route->category($child);
            $item_count = ($this->params->get('template.show_sub_categories_item_count')) ? ' <span>('.$child->totalItemCount().')</span>' : '';
                echo '<li><a href="'.$link.'" title="'.$child->name.'">'.$child->name.'</a>'.$item_count.'</li>';
        }
    } // line I added for while loop
    ?>
</ul>
4

1 回答 1

1

在达到限制时foreach跟踪:break;

<ul class="sub-categories">
    <?php
        $i = 0;
        foreach ($category->getChildren() as $child) {
            if (!$child->totalItemCount()) continue;
            $i++; if($i>5) break;
            $link = $this->app->route->category($child);
            $item_count = ($this->params->get('template.show_sub_categories_item_count')) ? ' <span>('.$child->totalItemCount().')</span>' : '';
            echo '<li><a href="'.$link.'" title="'.$child->name.'">'.$child->name.'</a>'.$item_count.'</li>';
        }
    ?>
</ul>
于 2012-07-19T17:08:28.467 回答