草庐IT

php - Wordpress WooCommerce 显示单个产品的嵌套产品类别

coder 2024-05-03 原文

我有一个销售汽车零件的 Wordpress WooCommerce 网站。对于每个零件(产品),我都创建了可以分配给该零件的唯一产品类别。所以例如头灯(部分)可以来自 3-Door 1999 Blue Alfa Romeo 156 1.1 Petrol Manual。

在单个产品页面上,我想显示一个仅包含与此部分关联的产品类别的嵌套列表。所以当我标记一个部分时,我将有一个如下图所示的嵌套 View 。

但是,我当前在第二张图片下方的代码显示了所有与其关联的部分(包括本部分)的产品类别。从下面的第二张图片可以看出,我有许多其他零件分配给其他汽车品牌,它们都显示在这个零件上。我只希望这部分显示与这部分相关联的产品类别。因此,在 Make 下,它应该只显示阿尔法罗密欧 - 而不是所有其他包含零件的产品类别,无论它们是否标记在此零件中。

有人可以帮忙吗?

当前代码

<?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它的灵 active 。它不仅适用于内置类别,还适用于自定义分类法

这是手抄本中的一个例子

<?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 显示单个产品的嵌套产品类别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25998739/

有关php - Wordpress WooCommerce 显示单个产品的嵌套产品类别的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  4. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  5. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  6. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  7. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  8. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  9. ruby-on-rails - 使用回形针的嵌套形式 - 2

    我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?

  10. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

随机推荐