这是为了显示每个产品类别的名称和图像,并带有指向类别woocommerce页面的链接。要以动态方式自动获取它,可以使用函数get\\u terms()。这只显示关联了产品的类别,要显示所有类别,甚至是空类别,我们需要在这个$cat\\u args变量数组中将“hide\\u empty”更改为false。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
echo \'<pre>\';
print_r($cats);
有了这个,我们可以循环显示预期的信息。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
echo $cat->name;
endforeach;
例如,显示;“第一代”;类别我们比较父项等于零。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0):
echo $cat->name;
endif;
endforeach;
如果我们有分类图像,我们可以这样显示它们。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0):
$image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
echo $cat->name;
endif;
endforeach;
现在让我们假设类别有子类别(“第二代”),我们可以在foreach中创建foreach循环。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0):
$image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
echo $cat->name;
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id):
$image = wp_get_attachment_url(get_term_meta($cat2->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
echo $cat2->name;
endif;
endforeach;
endif;
endforeach;
也可以有更多的子类别,比如“a”;“第三代”;,只需在第二个foreach循环中编写第三个foreach循环。
$cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args );
foreach ($cats as $key => $cat):
if ($cat->parent == 0):
$image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
echo $cat->name;
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id):
$image = wp_get_attachment_url(get_term_meta($cat2->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
echo $cat2->name;
foreach ($cats as $key => $cat3):
if ($cat3->parent == $cat2->term_id):
$image = wp_get_attachment_url(get_term_meta($cat3->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif;
echo $cat3->name;
endif;
endforeach;
endif;
endforeach;
endif;
endforeach;
要将链接添加到类别页面,可以使用函数get\\u term\\u link()。
<a href="<?php echo get_term_link($cat); ?>"> <?php echo $cat->name ?></a>
如果有更多的子类别的子类别,这可以继续下去,但从用户的角度来看,我相信超过三个“;“世代”;就类别而言,这已经足够好了,比这更多的内容可能开始变得非常混乱。
在这个动态的产品类别菜单中或我们展示的任何东西中,例如在主页中,如果我们添加或删除一个类别,它们将显示或不显示原样。
这就是我使用的函数,现在我们只需要在上面添加html和css甚至javascript!
这里只是它的一个小结构。
<?php $cat_args = array( \'orderby\' => \'name\', \'order\' => \'asc\', \'hide_empty\' => true,);
$cats = get_terms( \'product_cat\', $cat_args ); ?>
<div class="menu-body"> <?php
foreach ($cats as $key => $cat):
if ($cat->parent == 0): ?>
<div class="menu-cat"> <?php
$image = wp_get_attachment_url(get_term_meta($cat->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif; ?>
<a href="<?php echo get_term_link($cat); ?>"> <?php echo $cat->name ?></a> <?php
foreach ($cats as $key => $cat2):
if ($cat2->parent == $cat->term_id): ?>
<div class="menu-subcat"> <?php
$image = wp_get_attachment_url(get_term_meta($cat2->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif; ?>
<a href="<?php echo get_term_link($cat2); ?>"> <?php echo $cat2->name ?></a> <?php
foreach ($cats as $key => $cat3):
if ($cat3->parent == $cat2->term_id): ?>
<div class="menu-sub-subcat"> <?php
$image = wp_get_attachment_url(get_term_meta($cat3->term_id, \'thumbnail_id\', true));
if ( $image ): echo \'<img src="\'.$image.\'" alt=""/>\'; endif; ?>
<a href="<?php echo get_term_link($cat); ?>"> <?php echo $cat->name ?></a>
</div> <?php
endif;
endforeach; ?>
</div> <?php
endif;
endforeach; ?>
</div> <?php
endif;
endforeach; ?>
</div>
<style media="screen">
.menu-body{
background:gray;
margin:10px;
border-radius:10px;
}
.menu-body img{
width:30px;
height:30px;
}
.menu-cat{
margin-left:15px;
color:yellow;
}
.menu-subcat{
margin-left:30px;
color:lightgreen;
}
.menu-sub-subcat{
margin-left:30px;
color:lightblue;
}
</style>