2014-04-23 29 views
1

我正在研究wordpress + woocommerce上的内容。 我输入了一些产品的“过滤器”属性值。如果属性值等于...显示的东西

因此,如果此产品的属性值包含“Wood Grains”值,我想在前端回显Woodgrains.jpg。

因此,如果value = Texture,我想回显Texture.jpg图标。

下面的代码是我迄今为止编写的代码,但是这只能回应所有标记为产品的值,我无法弄清楚如何更改以获取'if'语句。

$terms = get_the_terms($product->id, 'pa_filter'); 
foreach ($terms as $term) { 
echo $term->name; 
} 

这里是一个什么样的代码在前端上面做了截图: http://edleuro.com/new/wp-content/themes/mystile/img/1.png

回答

0

我找到了解决我的问题的更好方法。我在我的content-product.php woocommerce模板中有这个。

<?php 
    $terms = get_the_terms($product->id, 'pa_filter'); 
    foreach($terms as $term){ 
    if($term->name == 'Wood Grains'){ 
     echo '<img src="icon_woodgrains.gif">'; 
    } 
    } 
?> 

请注意,术语名称区分大小写。

2

如果返回上述乘积项的数组:

$terms = get_the_terms($product->id, 'pa_filter'); 

您可以检查返回的结果数组有你正在寻找的这样做:

if (in_array("Wood Grains", $terms)) 
{ 
    // has it 
} 
else 
{ 
    // doesn't have it 
} 

更新根据您的回答我的答案

,我想出了以下内容:

创建一个辅助函数是这样的:

function termExists($myTerm, $terms) 
{ 
    if (is_array($terms)) { 
     foreach ($terms as $id => $data) { 
      if ($data->name == $myTerm) 
       return true; 
     } 
    } 
    return false; 
} 

然后你像这样使用它:

if (termExists('Wood Grains', get_the_terms($product->id, 'pa_filter'))) 
{ 
    // term exists 
} 
else 
{ 
    // does not exists 
} 
+0

对不起,所以..我试过,但我不知道什么是错的。你写的一行代码是替换我最初写在上面的整个代码?我对么? 这样的事情? 'if(in_array(“Wood Grains”,get_the_terms($ product-> id,'pa_filter'))) { \t echo“祝您有美好的一天! }' – wsgg

+0

我已经更新了我的帖子,关于一个班轮。是;这就是你如何使用它。你试过了吗? – Latheesan

+0

是的,我已经尝试过更新的单线。但不幸的是它没有回应任何事情。我会尝试使用代码来查看是否有任何内容出现。谢谢 – wsgg

相关问题