在4列自定义菜单中显示WooCommerce产品类别

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

我试图在下拉菜单中列出一定数量的产品类别(x),但如果产品类别总数大于x,则只列出x减去1,并在最后显示“查看所有类别”链接。我试图实现的是每列只有8个条目(包括查看所有链接),总共有4列。

我是个初学者,已经用尽了if/else逻辑的知识。我尝试过的一切都把结果搞砸了。

下面是基本代码,它以8列的形式列出它们,总计32列。我想如果有33个或更多的类别,第32个类别将成为所有类别的链接。如果只有32个,那么只列出所有32个,没有链接。

<?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网友:LoicTheAztec

以下内容将在4列中显示最多32个术语(产品类别),因此按列显示8个项目。如果有32个以上的术语,则将第32个术语替换为“查看类别”自定义链接项:

<?php

## Settings
$items_per_col  = 8; // Number of items by column
$number_of_cols = 4; // Number of columns
$taxonomy       = \'product_cat\'; // WooCommerce product category taxonomy
$excluded_ids   = \'73, 74, 16\'; // Excluding "best sellers", "New" and "Uncategorized" terms Ids

## Initializing variables
$col_count = 1;
$counter   = 0; 
$html      = \'\';

$max_items = $number_of_cols * $items_per_col; // Max number of items (calculation)

$terms_list = wp_list_categories( array(
    \'taxonomy\'     => $taxonomy,
    \'number\'       => ( $max_items + 1 ), // 33 max
    \'title_li\'     => \'\',  // disabled
    \'pad_counts\'   => 0,   // no
    \'hide_empty\'   => 0,   // no
    \'echo\'         => 0,   // return (not echo)
    \'style\'        => \'\',  // Without style (bullets)
    \'depth\'        => \'1\', // Top level terms only
    \'exclude\'      => $excluded_ids,
) );

$terms_array = explode( "<br />", $terms_list ); // Split each item in an array of terms

array_pop($terms_array); // <== We remove last empty item from the array.

$items_count = count($terms_array); // get items (terms) count

// Loop through product categories terms
foreach ( $terms_array as $term_html ) {
    if ( $counter == 0 ) { // column start
        $html .= \'<div class="cat-columns" id="cat-col-\' . $col_count . \'" style="float:left; margin: 0 10px;">\';
    }

    if( $items_count > $max_items && $col_count == $number_of_cols && $counter == ($items_per_col - 1) ) {
        $link    = home_url( "/all-categories/", "https" );
        $html .= \'<div><a href="\' . $link . \'">\' . __("View Categories") . \'</a></div>\';
    }
    else {
        $html .= \'<div data-counter="\'.$counter.\'">\' . $term_html . \'</div>\';
    }

    $counter++; // Increase items counter

    if ( $counter == $items_per_col ) { // column end
        $html .= \'</div>\';

        $counter = 0; // Reset items counter
        $col_count++; // Increase colum count

        if ( $col_count > $number_of_cols ) {
            break; // Stop the loop (to be sure)
        }
    }
}

## html output
echo \'<div class="categories-wrapper">\' . $html. \'</div>\'; 
?>
已测试并正常工作。