2 回答
TA贡献1864条经验 获得超2个赞
tax_query takes an array of tax query arguments arrays (it takes an array of arrays) but you are using only single array. The correct code is as following
add_action( 'wp_ajax_nopriv_load-products-default',
'prefix_load_default_cat_posts' );
add_action( 'wp_ajax_load-products-default', 'prefix_load_default_cat_posts' );
function prefix_load_default_cat_posts(){
$slug = $_POST['cat'];
$categories = get_terms(array('tshirt'),array('hide_empty' => false));
foreach( $categories as $cat )
{
$args = array(
'post_type' => 'products',
'posts_per_page' => 1,
'tax_query' => array( array('taxonomy' => 'tshirt','field' => 'slug',
'terms' => $slug)));
$q = new WP_Query($args);
if( $q->have_posts()):
while( $q->have_posts()):
$q->the_post();
echo 'post here';
endwhile;
endif;
}
}
TA贡献1876条经验 获得超7个赞
是的,所以我的代码设置不正确......这是在自定义类别中获取第一个自定义帖子的正确方法:
add_action( 'wp_ajax_nopriv_load-products-default', 'prefix_load_default_cat_posts' );
add_action( 'wp_ajax_load-products-default', 'prefix_load_default_cat_posts' );
function prefix_load_default_cat_posts(){
$slug = $_POST['cat'];
// echo $slug;
$args = array(
'post_type' => 'products',
'posts_per_page' => 1,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'brand',
'field' => 'slug',
'terms' => $slug
),
),
);
$q = new WP_Query($args);
if( $q->have_posts()):
while( $q->have_posts()):
$q->the_post();
the_title();
endwhile;
endif;
die();
}
?>
- 2 回答
- 0 关注
- 152 浏览
添加回答
举报