2016-11-14 114 views
5

我一直在研究关于我的问题的所有网络和论坛,但我似乎无法产生正确的结果。基本上我试图仅显示特定产品类别的条款或产品属性。显示特定产品类别的条款或产品属性(woocommerce)

这是我一直在努力的代码。

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

你可以共享'的var_dump($计算)',所以我们可以看到数据的格式?另外,你会得到什么输出?任何错误?确保设置error_reporting(E_ALL); – mseifert

+0

用更多的细节更新了我的问题 – clestcruz

回答

3

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

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

我认为这不会起作用 – clestcruz

+0

请检查您的代码,您的代码有错误。声明应该以;以及为什么你使用<?php echo“。” 。 $ term-> slug?>。这是不正确的。 –

+0

并且get_terms('wine');足够。 –

3

尝试这样:

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

试过你的解决方案,但没有运气 – clestcruz

4

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; 

使你术语名称:例如术语名称实例

$name = $term->name; 

首先,确保你使用的是正确的版本 - 使用的是从之前4.5.0

二语法,错误是说,分类pa_liquor-types是无效的。你需要检查这个被定义的位置。

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

0

分别获得葡萄酒和白酒类型的条款,将它们合并到单个数组条目中并通过该数组循环以获得所有条款。希望它有帮助,还没有测试过代码。

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

不知道你是否有$发布或没有,如果没有获得当前产品的条件你想列出和取代$发布产品的ID。 – Yamu