下面带有“\'taxonomy\'=>\'age\'”的数组不起作用,如果我复制并粘贴数组“array(\'19\',\'20\')”来代替变量,它就可以正常工作。我刚开始学习PHP,所以请对我放松:)
if($_REQUEST["price"] == 19){$price = "\'19\'";}
elseif($_REQUEST["price"] == 20){$price = "array( \'19\', \'20\' )";}
elseif($_REQUEST["price"] == 21){$price = "array( \'19\', \'20\', \'21\' )";}
elseif($_REQUEST["price"] == 22){$price = "array( \'19\', \'20\', \'21\', \'22\' )";}
elseif($_REQUEST["price"] == 23){$price = "array( \'19\', \'20\', \'21\', \'22\', \'23\' )";}
echo $price;
$my_query = new WP_Query( array(
\'post_status\' => \'publish\',
\'post_type\' => \'post\',
\'cat\' => \'\' . $_REQUEST["category"] . \'\',
\'tax_query\' => array(
\'relation\' => \'OR\',
array(
\'taxonomy\' => \'age\',
\'field\' => \'id\',
\'terms\' => \'\' . $_REQUEST["age"] . \'\'
),
array(
\'taxonomy\' => \'price\',
\'field\' => \'id\',
\'terms\' => $price,
\'operator\' => \'IN\'
),
array(
\'taxonomy\' => \'group\',
\'field\' => \'id\',
\'terms\' => \'\' . $_REQUEST["group"] . \'\'
)
),
\'posts_per_page\' => \'-1\'
) );
最合适的回答,由SO网友:s_ha_dum 整理而成
您的数组定义不正确。这
elseif($_REQUEST["price"] == 20){$price = "array( \'19\', \'20\' )";}
。。。创建读取“array(\'19\',20\')”的文本字符串。删除引号。
elseif($_REQUEST["price"] == 20){$price = array( \'19\', \'20\' );}
这将为您提供PHP数组。