简体   繁体   English

显示特定产品类别的术语或产品属性(woocommerce)

[英]Display the terms or product attributes for a specific product category(woocommerce)

I have been researching all over the net and forums regarding my question but I can't seem to produce the correct results. 我一直在研究关于我的问题的网络和论坛,但我似乎无法产生正确的结果。 Basically I'm trying to display the terms or product attributes for only a specific product category. 基本上我只想显示特定产品类别的术语或产品属性。

Here is the code I have been working on. 这是我一直在研究的代码。

<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
        (
        )
)

The var_dump is showing that you are using taxonomies on WordPress. var_dump显示您正在使用WordPress上的分类法。 While I don't have experience directly with Wordpress, the Wordpress site docs say: 虽然我没有直接使用Wordpress的经验, 但Wordpress网站的文档说:

Prior to 4.5.0, the first parameter of get_terms() was a taxonomy or list of taxonomies: 在4.5.0之前,get_terms()的第一个参数是分类法或分类法列表:

Since 4.5.0, taxonomies should be passed via the 'taxonomy' argument in the $args array: 从4.5.0开始,分类法应该通过$ args数组中的'taxonomy'参数传递:

From the function reference: 从功能参考:

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

Gives you term slug: eg: term-slug-example 给你一个术语slug:例如:term-slug-example

$slug = $term->slug;

Gives you term name: eg Term Name Example 为您提供术语名称:例如术语名称示例

$name = $term->name; 

First, make sure you are using the correct version - you are using the syntax from prior 4.5.0 首先,确保使用的是正确的版本 - 您使用的是之前的4.5.0语法

Second, the error is saying that the taxonomy pa_liquor-types is invalid. 其次,错误是分类标准pa_liquor-types无效。 You need to check where this is being defined. 您需要检查定义的位置。

Check your create_initial_taxonomies() function syntax and post it if necessary. 检查create_initial_taxonomies()函数语法并在必要时发布。

try something like this: 尝试这样的事情:

<?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 } ?>

Use ; 采用 ; after $term->slug and $term->name. 在$ term-> slug和$ term-> name之后。

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

Get terms of wine and liquor-types seperately, merge them to single array $terms and loop through that array to get all terms. 单独获取葡萄酒和酒类的条款,将它们合并为单个数组$ term并循环遍历该数组以获得所有条款。 Hope it helps, haven't tested the code yet. 希望它有所帮助,尚未测试代码。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在特定的 Woocommerce 产品类别档案页面上显示产品属性 - Display product attributes on specific Woocommerce product category archives page 在 Woocommerce 档案中显示可变产品属性和术语 - Display Variable Product Attributes and Terms on Woocommerce Archives 获取与 Woocommerce 中的产品类别相关的所有属性及其术语 - Get all attributes with their terms related to a product category in Woocommerce 显示 WooCommerce 产品的特定产品属性链接术语集 - Display specific product attribute linked terms set for a WooCommerce product 显示Woocommerce中特定产品类别的产品 - Display products from specific product category in Woocommerce WooCommerce 中特定产品类别的自定义条款和条件复选框 - Custom terms and conditions checkbox for a specific product category in WooCommerce 在 Woocommerce 档案页面上显示特定的产品属性 - Display specific product attributes on Woocommerce archives pages 在基于父项的Woocommerce产品上显示特定的产品子类别标题 - Display specific product child category title on Woocommerce product based on parent 显示 WooCommerce 自定义产品属性和单个产品的所有条款 - Display WooCommerce Custom Product Attributes and all terms on single products 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM