php-WordPress WooCommerce显示单个产品的嵌套产品类别
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了php-WordPress WooCommerce显示单个产品的嵌套产品类别,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2500字,纯文字阅读大概需要4分钟。
内容图文
![php-WordPress WooCommerce显示单个产品的嵌套产品类别](/upload/InfoBanner/zyjiaocheng/680/c355eb81b77d4431a3b09f4ce7e129d9.jpg)
我有一个Wordpress WooCommerce网站,销售汽车零件.对于每个零件(产品),我创建了可以分配给零件的唯一产品类别.所以例如前大灯(零件)可以来自3门1999 Blue Alfa Romeo 156 1.1汽油手册.
在单个产品页面上,我只想显示与该零件关联的产品类别的嵌套列表.因此,当我标记零件时,将有一个嵌套视图,如下图所示.
但是,第二幅图像下方的当前代码显示了所有与之相关的产品类别,包括THIS部分.从下面的第二张图片中可以看出,我将许多其他零件分配给其他汽车品牌,它们全部显示为该零件.我只希望该零件显示与此零件关联的产品类别.因此,在“制作”下仅应显示Alfa Romeo-而不显示其中包含零件的所有其他产品类别,无论它们是否在此零件中进行了标记.
谁能帮忙吗?
当前代码
<?php
$woocCategoryTerms = get_terms('product_cat', array(
'order' => 'ASC',
'hide_empty' => true, // (boolean)
'parent' => 0, // (integer) Get direct children of this term (only terms whose explicit parent is this value). If 0 is passed, only top-level terms are returned. Default is an empty string.
'hierarchical' => true, // (boolean) Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true).
));
foreach($woocCategoryTerms as $wooCategoryTerm) :
?>
<ul>
<li>
<a href="<?php echo get_term_link( $wooCategoryTerm -> slug, $wooCategoryTerm -> taxonomy ); ?>">
<?php
echo $wooCategoryTerm -> name;
?>
</a>
<ul class="wsubcategs">
<?php
$wooSubArgs = array(
'hierarchical' => true,
'hide_empty' => true,
'parent' => $wooCategoryTerm -> term_id,
'taxonomy' => 'product_cat'
);
$wooSubCategories = get_categories($wooSubArgs);
foreach ($wooSubCategories as $wooSubCategory):
?>
<li>
<a href="<?php echo get_term_link( $wooSubCategory -> slug, $wooSubCategory -> taxonomy );?>">
<?php
echo $wooSubCategory -> name;
?>
</a>
</li>
<?php
endforeach;
?>
</ul>
</li>
</ul>
<?php
endforeach;
?>
解决方法:
get_terms返回给定特定分类法的所有术语,而不是帖子.您可以在这里选择两个,但我喜欢使用wp_list_categories的灵活性.它不仅适用于构建类别,还适用于自定义分类法
这是来自法典的例子
<?php
$taxonomy = 'category'; //change to your taxonomy name
// get the term IDs assigned to post.
$post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// separator between links
$separator = ', ';
if (?!empty( $post_terms ) &&?!is_wp_error( $post_terms ) ) {
$term_ids = implode( ',' , $post_terms );
$terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
$terms = rtrim( trim( str_replace( '<br />', $separator, $terms ) ), $separator );
// display post categories
echo $terms;
}
?>
您也可以使用get_the_terms
内容总结
以上是互联网集市为您收集整理的php-WordPress WooCommerce显示单个产品的嵌套产品类别全部内容,希望文章能够帮你解决php-WordPress WooCommerce显示单个产品的嵌套产品类别所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。