我试图在下拉菜单中列出一组产品类别(x),但是如果产品类别的总量大于x,则只列出x减去1并显示“查看所有类别”链接。我试图实现的是,每列只有8个条目(包括查看所有链接),共有4列。
我是一个初学者,我已经用尽了我的知识,如果/其他逻辑。我所做的一切都把结果搞砸了。
下面是在8列中列出它们的基本代码,总共为32列。Id喜欢,如果有33个或更多的类别,为32是一个链接到所有他们。如果只有32,那么只需列出所有32没有链接。
'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("
",$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;
?>
= $cats_per_list) {
$result_number = 0;
$list_number++;
echo ''.$category.' ';
}
else {
echo ''.$category.'';
}
}
echo 'View Categories';
?> 发布于 2019-09-07 15:23:59
以下将显示在4列,32术语,最多在所有(产品类别),所以8个项目的一栏。如果有超过32个术语,则将第32项替换为“查看类别”自定义链接项:
$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( "
", $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 .= '';
}
if( $items_count > $max_items && $col_count == $number_of_cols && $counter == ($items_per_col - 1) ) {
$link = home_url( "/all-categories/", "https" );
$html .= '' . __("View Categories") . '';
}
else {
$html .= '' . $term_html . '';
}
$counter++; // Increase items counter
if ( $counter == $items_per_col ) { // column end
$html .= '';
$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 '' . $html. '';
?>测试和工作。
https://wordpress.stackexchange.com/questions/346801
复制相似问题