我想知道你是否可以帮助处理以下问题。
我有一个字符串,选择产品从我的WP woocommerce网站有一定的分类法附加到他们。我想要的是在数组中包含一个变量。
我现在有的是这个,效果很好:
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'pa_garment',
'field' => 'tag_ID',
'terms' => '59'
),
array(
'taxonomy' => 'pa_audience',
'field' => 'tag_ID',
'terms' => '39'
)
),
'orderby' => 'rand'
);
$args = $args;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
} {
$image = print_r(get_the_post_thumbnail($loop->post->ID, 'shop_catalog'));
}
$image = $image[0];
$image = apply_filters( 'woocommerce_placeholder_img_src',$image[0] );
但是我想插入变量$category1和$category2,而不是指定类别"39"answers"59"。
所以我试过这个,但它似乎不工作:
$args = array(
'post_type' => 'product',
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'pa_garment',
'field' => 'tag_ID',
'terms' => $category1
),
array(
'taxonomy' => 'pa_audience',
'field' => 'tag_ID',
'terms' => $category2
)
),
'orderby' => 'rand'
);
$args = $args;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
} {
$image = print_r(get_the_post_thumbnail($loop->post->ID, 'shop_catalog'));
}
$image = $image[0];
$image = apply_filters( 'woocommerce_placeholder_img_src',$image[0] );
我已经尝试在变量($$category1)的开头添加额外的$,也添加额外的括号${$category1},但我似乎无法弄清楚。
请帮忙!
干杯克里斯。
$category 1和$category 2必须是数组,因此首先需要获得与分类法
相关的术语$category1 = array('term1', 'term2');
或术语id:
$category1 = array(1,2,3);
使用函数get_terms($taxonomies, $args)获取一个术语数组