to convert it from category id to category name
when I echo $slidecat it shows cat id of “burger” which is 16
You can use get_term_field()
to get the term/category slug by ID:
// Get the slug and then pass it to WP_Query.
$cat_slug = get_term_field( 'slug', $slidecat, 'product_cat' );
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'product_cat' => $cat_slug,
'orderby' => 'date',
'order' => 'ASC'
);
Or you could actually just pass the category ID to WP_Query
using the taxonomy query, like so:
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
// 'product_cat' => 'burger', // don't set product_cat
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $slidecat,
'field' => 'term_id',
),
),
'orderby' => 'date',
'order' => 'ASC'
);
Additionally, you should replace the wp_reset_query()
in your code with wp_reset_postdata()
because the former is used only if the main WordPress query is modified (e.g. if you called query_posts()
which should just be avoided).