列出最多x个产品类别,每列数量为y

时间:2019-09-08 作者:aye cee

我试图列出最多“x”个产品类别(y的倍数),每列的数量为“y”x’决定显示多少列。

我想做的是:-如果商店中的产品类别少于“x”,则显示所有。-如果正好有“x”,则显示“x”。-如果有多个“x”,则显示x-1并在最后一个位置显示“查看所有类别”链接。

例如:有35个产品类别。如果最大产品类别=32,每列数量=8,则在4列中显示31个产品类别,并在第32位显示“全部查看”链接。

总的产品类别在不断变化,因此我尝试使代码以任何数量动态工作,而无需更改代码,除非更改“x”或“y”。

这是我能够为自己编写的基本代码,但是链接会溢出到下一列,而不是上一列的最后一个位置。作为一名初学者,我已经用尽了if/else逻辑的知识,我尝试过的其他一切都会把输出搞砸。

<?php

$args = array(
         \'taxonomy\'     => \'product_cat\',
         \'orderby\'      => \'name\',
         \'number\'       => 32,   //maximum to list
         \'title_li\'     => \'\',
         \'show_count\'   => 0,    // 1 for yes, 0 for no
         \'pad_counts\'   => 0,    // 1 for yes, 0 for no
         \'hierarchical\' => 1,    // 1 for yes, 0 for no
         \'hide_empty\'   => 0,    // 1 for yes, 0 for no
         \'echo\'  => 0,           // 1 for yes, 0 for no
         \'exclude\' => \'73, 74, 16\', //best sellers, new, and uncategorized
         \'depth\'  => \'1\',        //top level categories, not sub
         \'style\'  => \'\',         //default is list with bullets, \'\' is without
  );

// Grab top level categories
$get_cats = wp_list_categories($args);

// Split into array items
$cat_array = explode("<br />",$get_cats);

// Amount of categories (count of items in array)
$results_total = count($cat_array);

// How many tags to show per list-8)
$remainder = ($results_total-8);
$cats_per_list = ($results_total-$remainder);

// Counter number for tagging onto each list
$list_number = 1;

// Set the category result counter to zero
$result_number = 0;
?>

<div class="cat_columns" id="cat-col-<?php echo $list_number; ?>">

<?php     
foreach($cat_array as $category) {        
                $result_number++;

if($result_number >= $cats_per_list) { 
     $result_number = 0; 
     $list_number++;
    echo \'<div>\'.$category.\'</div> </div> <div class="cat_columns" id="cat-col-\'.$list_number.\'">\';
}
 else {         
 echo \'<div>\'.$category.\'</div>\'; 
}
}
echo \'<a href="https://www.aaaa.com/all-categories//">View Categories</a>\';
 ?> 

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

因此,这与您的代码(以及另一个问题的公认答案中的代码)不同,但对我来说效果很好:

您可以删除"\\t" .. "\\n" 为了更好地检查生成的HTML标记,我添加了它,以便于格式化

// First, define these.
$max_cat_count = 32; // this is \'x\'
$qty_per_column = 8; // this is \'y\'

// Then the $args array.
$args = array(
    \'taxonomy\'   => \'product_cat\',
    \'number\'     => $max_cat_count + 1, // keep the + 1
    \'title_li\'   => \'\',
    \'hide_empty\' => 0,
    \'echo\'       => 0,
    \'style\'      => \'\',
    // ... your other args here ...
);

// Get the categories list.
$get_cats = wp_list_categories( $args );

// Split the list into array items.
$cat_array = explode( "<br />", $get_cats );

$total = count( $cat_array );
$list_number = 1;
$_new_col = false;

foreach ( $cat_array as $i => $category ) {
    if ( $i >= $max_cat_count ) {
        break;
    }

    if ( $i % $qty_per_column === 0 ) {
        // Close previous column, if any.
        if ( $_new_col ) {
            echo \'</div><!-- .cat_columns -->\' . "\\n";
        }

        // Open new column.
        $id = \'cat-col-\' . $list_number;
        echo \'<div class="cat_columns" id="\' . $id . \'">\' . "\\n";

        $_new_col = true;
        $list_number++; // increment the columns count
    }

    if ( $total > $max_cat_count && $i === $max_cat_count - 1 ) {
        // Make sure to change the # to the proper URL.
        echo "\\t" . \'<div><a href="#">View All</a></div>\' . "\\n";
    } else {
        echo "\\t" . \'<div>\' . trim( $category ) . \'</div>\' . "\\n";
    }
}
// Close last column, if any.
if ( $_new_col ) {
    echo \'</div><!-- .cat_columns -->\' . "\\n";
}