php - Woocommerce 类别限制
问题描述
我想知道如何限制 wordpress woocommerce 类别显示。我只想显示前 3 个类别,但此代码显示所有类别。我不想显示水球类别
`<?php
$taxonomy = 'product_cat';
$orderby = '';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>`
我只想在主页上显示 3 个类别(其中任何一个都是随机的)
解决方案
您需要添加数字参数来限制类别。
'number' => 3,
你的完整代码
<?php
$taxonomy = 'product_cat';
$orderby = '';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty,
'number' => 3,
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
if($cat->category_parent == 0) {
$category_id = $cat->term_id;
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
?>
推荐阅读
- ruby-on-rails - Rails如何在参数中封装2个var?
- java - 使用非静态内部类在 MainActivity 中接收广播
- python-3.x - 在 Linux 命令中使用命令“import os”失败?
- c# - 广义 DocumentDB / CosmosDB 分区策略
- javascript - jquery加载多行
- qt - Seg Fault 创建新的 QTcpSocket
- java - 无法解析符号“MODE_PRIVATE”
- java - 如何确定哪个 Xlint 选项对应于编译器警告?
- python - 为什么python的'不是'不一致?
- c# - 创建使用一个 foreach 而不是多个的方法