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

cle*_*ruz 6 php loops woocommerce taxonomy-terms

我一直在研究关于我的问题的网络和论坛,但我似乎无法产生正确的结果.基本上我只想显示特定产品类别的术语或产品属性.

这是我一直在研究的代码.

<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
        (
        )
)
Run Code Online (Sandbox Code Playgroud)

mse*_*ert 5

var_dump表明您正在 WordPress 上使用分类法。虽然我没有直接使用 Wordpress 的经验,但Wordpress 网站文档说:

\n\n
\n

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

\n\n

从 4.5.0 开始,分类法应通过 $args 数组中的 \xe2\x80\x98taxonomy\xe2\x80\x99 参数\n 传递:

\n
\n\n

从函数参考:

\n\n
$term = get_term( $term_id, $taxonomy );\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
$slug = $term->slug;\n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n
$name = $term->name; \n
Run Code Online (Sandbox Code Playgroud)\n\n

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

\n\n

其次,错误在于分类法pa_liquor-types无效。您需要检查它的定义位置。

\n\n

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

\n


Jom*_* MJ 2

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

 <label> 
        <input type="checkbox" value="<?php echo $term->slug; ?>"  /> 
        <?php echo $term->name; ?>
    </label>
Run Code Online (Sandbox Code Playgroud)