调整PHP代码以将列表拆分为大致相等的comuns

时间:2018-02-23 13:00:33

标签: php list multiple-columns

我有现有的PHP代码,可以将产品类别列为链接列表:

    <?php // as per https://phptechnologytutorials.wordpress.com/2015/11/07/get-
list-of-all-woocommerce-categories/
    $orderby = 'name';
    $order = 'asc';
    $hide_empty = false;
    $excluded = '32';
    $cat_args = array(
        'orderby'    => $orderby,
        'order'      => $order,
        'hide_empty' => $hide_empty,
        'exclude'    => $excluded,
    );

    $product_categories = get_terms( 'product_cat', $cat_args );

    if( !empty($product_categories) ){
        echo '<ul class="linked-product-category-list">';
        foreach ($product_categories as $key => $category) {
            echo '<li>';
            echo '<a href="'.get_term_link($category).'" >';
            echo $category->name;
            echo '</a>';
            echo '</li>';
        }
        echo '</ul>';
    }
    ?>

我发现另一个Stack Overflow solution演示了如何做我想要达到的目标 - 将项目列表排列成4个同样填充的列。

但是,我是PHP的新手,我不知道如何使解决方案适应我自己的代码。请帮助我使用下面解决方案的原则调整上面的代码,将我的列表拆分成列。

<?php
$col = 3;
$projects = array_chunk($projects, ceil(count($projects) / $col));

foreach ($projects as $i => $project_chunk)
{
    echo "<ul class='pcol{$i+1}'>";
    foreach ($project_chunk as $project)
        {
        echo "<li>{$project->name}</li>";
    };
    echo "</ul>";
}; ?>

0 个答案:

没有答案