Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果属性值等于。。。展示一些东西_Php_Wordpress_Attributes - Fatal编程技术网

Php 如果属性值等于。。。展示一些东西

Php 如果属性值等于。。。展示一些东西,php,wordpress,attributes,Php,Wordpress,Attributes,我正在wordpress+woocommerce上做一些事情。 我已经为一些产品输入了一些“过滤器”属性值 因此,如果该产品的属性值包括“Wood Grains”值,我想在前端回显Woodgrains.jpg 因此,如果值=Texture,我希望回显Texture.jpg图标 下面的代码是我到目前为止收集到的,但这只反映了标记到产品的所有值,我不知道如何更改以在其中获得“if”语句 $terms = get_the_terms( $product->id, 'pa_filter'); fo

我正在wordpress+woocommerce上做一些事情。 我已经为一些产品输入了一些“过滤器”属性值

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

因此,如果值=Texture,我希望回显Texture.jpg图标

下面的代码是我到目前为止收集到的,但这只反映了标记到产品的所有值,我不知道如何更改以在其中获得“if”语句

$terms = get_the_terms( $product->id, 'pa_filter');
foreach ( $terms as $term ) {
echo $term->name;
}
下面是上面代码在前端所做操作的屏幕截图:

如果返回所述产品的术语数组:

$terms = get_the_terms( $product->id, 'pa_filter');
通过执行以下操作,可以检查返回的结果数组是否具有所需的内容:

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

更新

根据你对我的答复,我得出以下结论:

创建如下所示的帮助器函数:

if (termExists('Wood Grains', get_the_terms($product->id, 'pa_filter')))
{
    // term exists
}
else
{
    // does not exists
}
然后你就这样使用它:

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

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

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


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

foreach打印警告:为foreach()提供的参数无效

这对我来说是“纯粹的”:

if(is_product()&&has_术语('Wood Grains','pa_filter')){
回声';
}

对不起,所以。。我试过了,但我不确定出了什么问题。您编写的一行代码是用来替换我最初在上面编写的整个代码?我说得对吗?像这样的<代码>如果(在数组中(“木纹”,获取术语($product->id,'pa\u filter')){echo“祝您愉快!”}我已经更新了关于一行的帖子。对你就是这么用的。你试过了吗?是的,我试过最新的一款。但不幸的是,它没有回应任何东西。我会尝试修改代码,看看是否有结果。谢谢,别理那一行。这样做
$terms=get_the_terms($product->id,'pa_filter')然后像我显示的那样检查。如果仍然不起作用,你能
var\u dump($terms)-我想看看它的内容。我不太明白你的“做这个…”无论如何做了一个var转储。这里是输出
array(1){[20]=>object(stdClass){277(11){[“term\u id”]=>int(20)[“name”]=>string(11)“Wood Grains”[“slug”]=>string(11)“Wood Grains”[“term\u group”]=>int(0)[“term\u taxonomology\u-id”=>int(21)[“taxonomology”=>string(9)“pa-u-filter”[“description”]=>string(0)”[“parent”=>int(0)”父项”=>int(0)=>int)[id”=>int(13]对象)[“过滤器”]=>字符串(3)“原始”}
if ( is_product() && has_term( 'Wood Grains', 'pa_filter' )) {
    echo '<img src="Texture.jpg">';
}