【问题标题】:Display the terms or product attributes for a specific product category(woocommerce)显示特定产品类别的术语或产品属性(woocommerce)
【发布时间】:2017-03-27 15:45:07
【问题描述】:

我一直在网上和论坛上就我的问题进行研究,但似乎无法得出正确的结果。基本上,我试图仅显示特定产品类别的条款或产品属性。

这是我一直在处理的代码。

<fieldset class="liquor-types-control filter-controls" >

    <?php 

    $terms = get_terms( 'wine', /*<--Product Category */ 'pa_liquor-types' /* <--Product Attribute */);
    foreach ( $terms as $term ) :
    ?>

    <label> 
        <input type="checkbox" value="<?php echo "." . $term->slug ?>"  /> 
        <?php echo $term->name ?>
    </label>

    <?php endforeach; ?>


</fieldset>


(
    [errors] => Array
        (
            [invalid_taxonomy] => Array
                (
                    [0] => Invalid taxonomy.
                )

        )

    [error_data] => Array
        (
        )
)

【问题讨论】:

  • 你能分享一个var_dump($terms) 以便我们查看数据的格式吗?另外,你得到什么输出?有什么错误吗?确保设置 error_reporting(E_ALL);
  • 用更多细节更新了我的问题
  • 您找到解决方案了吗?

标签: php loops woocommerce taxonomy-terms


【解决方案1】:

var_dump 表明您在 WordPress 上使用分类法。虽然我没有直接使用 Wordpress 的经验,但the Wordpress site docs 说:

在 4.5.0 之前,get_terms() 的第一个参数是分类或 分类列表:

自 4.5.0 起,分类法应通过“分类法”参数传递 在 $args 数组中:

From the function reference:

$term = get_term( $term_id, $taxonomy );

为您提供术语 slug:例如:term-slug-example

$slug = $term->slug;

为您提供术语名称:例如术语名称示例

$name = $term->name; 

首先,确保您使用的是正确的版本 - 您使用的是 4.5.0 之前的语法

其次,错误是说分类法pa_liquor-types 无效。您需要检查这是在哪里定义的。

检查您的 create_initial_taxonomies() 函数语法并在必要时发布。

【讨论】:

    【解决方案2】:

    试试这样的:

    <?php
    $terms = get_terms( 'wine', 'pa_liquor-types');
    
    foreach($terms as $term) { ?>
        <input type="checkbox" value="<?php echo "." . $term['slug'];?>"/>
        <?php echo $term['name'];?>
    <?php } ?>
    

    【讨论】:

    • 尝试了您的解决方案,但没有成功
    【解决方案3】:

    使用;在 $term->slug 和 $term->name 之后。

     <label> 
            <input type="checkbox" value="<?php echo $term->slug; ?>"  /> 
            <?php echo $term->name; ?>
        </label>
    

    【讨论】:

    • 请检查您的代码,您的代码有错误。该语句应以 ; 结尾以及为什么要使用 蛞蝓?>。这是不正确的。
    • 还有 get_terms('wine');够了。
    • 不幸的是它似乎不起作用,我尝试了您的解决方案
    【解决方案4】:

    分别获取葡萄酒和烈酒类型的术语,将它们合并到单个数组 $terms 并遍历该数组以获取所有术语。希望对您有所帮助,尚未测试代码。

    <fieldset class="liquor-types-control filter-controls" >
    
        <?php 
        global $post;
        $terms_wine = get_the_terms(get_the_ID(),'wine');
        $terms_liquor = get_the_terms(get_the_ID,'pa_liquor-types');
        $terms = array();
        foreach($terms_wine as $wine){
            array_push($terms,$wind);
            }
        foreach($terms_liquor as $liquor){
            array_push($terms,$liquor);
            }
    
    
        foreach ( $terms as $term ) :
        ?>
    
        <label> 
            <input type="checkbox" value="<?php echo "." . $term->slug ?>"  /> 
            <?php echo $term->name ?>
        </label>
    
        <?php endforeach; ?>
    
    
    </fieldset>
    

    【讨论】:

    • 不确定您是否有 $post,如果没有,则获取要列出其条款的当前产品的 id,并将 $post 替换为产品的 id。
    猜你喜欢
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2020-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-06-10
    • 2021-01-05
    相关资源
    最近更新 更多