我正在研究一个项目并在不同类别的slug中获得相同的结果。请帮助我在这里做错了。
$act = $_POST['act'];
$args = array(
'posts_per_page' => 100,
'offset' => 0,
'category' => $act,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'product',
'taxnomy' => 'product_cat',
'post_status' => 'publish');
$myposts = get_posts( $args );
global $product;
global $wpdb;
foreach ($myposts as $key => $value) {
$id = $value->ID;
echo '<li class="product type-product status-publish has-post-thumbnail first grid with-hover add-hover open-on-mobile with-border span3 featured shipping-taxable product-type-simple product-cat-accommodation-durban product-cat-accommodation-battlefields product-cat-battlefields product-cat-comfortable-accommodation-durban product-cat-comfortable-accommodation-battlefields product-cat-durban instock">';
echo '<div class="product-wrapper">';
echo '<a class="thumb" href="'.get_permalink( $id ).'">';
$post_thumbnail_id = get_post_thumbnail_id($id);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
echo '<img src="'.$post_thumbnail_url.'" />';
echo '</a>';
echo '<h3>'.get_the_title( $id ).'</h3>';
echo '</div></li>';
}
答案 0 :(得分:1)
<?php
$slug = "category-b";
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $slug
)
),
'post_type' => 'product',
'orderby' => 'title',
);
$the_query = new WP_Query( $args );
foreach ($the_query->posts as $key => $value) {
print_r($value->ID);
}
?>
答案 1 :(得分:0)
经过慎重考虑,我找到了问题的根源:
注意:category参数必须是类别的ID,和 不是类别名称。
注意:category参数可以是逗号分隔的列表 类别,因为get_posts()函数传递'类别' 参数直接作为'cat'直接进入WP_Query。
注意:category_name参数必须是字符串,在本例中, 类别名称。