I have a theme with options. I would like to display 3 posts for a specific woocommerce category, I could show category id and I don't know how to display products based on its category id, but I have code snippet it shows products by woocommerce category name. So I need now to a selected category name not its id, or to convert it from category id to category name?
I tried following but it did not work at all. Any help will be apreciated, I'm struggling with this 2 days.
$slidecat =theme_get_option('theme_slide_categories');
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'product_cat' => 'burger',
'orderby' =>'date',
'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
the_title(); ?>
<?php endwhile;
wp_reset_query(); ?>
when I echo $slidecat it shows cat id of "burger" which is 16.
options area
$options[] = array( 'name' => __('Slider Category', 'theme'),
'desc' => __('Select a category for the featured post slider', 'theme'),
'id' => 'theme_slide_categories',
'type' => 'select',
'class' => 'hidden',
'options' => $options_categories);
I hope code I've supplied here is enough to get help. Thanks everybody
I have a theme with options. I would like to display 3 posts for a specific woocommerce category, I could show category id and I don't know how to display products based on its category id, but I have code snippet it shows products by woocommerce category name. So I need now to a selected category name not its id, or to convert it from category id to category name?
I tried following but it did not work at all. Any help will be apreciated, I'm struggling with this 2 days.
$slidecat =theme_get_option('theme_slide_categories');
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'product_cat' => 'burger',
'orderby' =>'date',
'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
the_title(); ?>
<?php endwhile;
wp_reset_query(); ?>
when I echo $slidecat it shows cat id of "burger" which is 16.
options area
$options[] = array( 'name' => __('Slider Category', 'theme'),
'desc' => __('Select a category for the featured post slider', 'theme'),
'id' => 'theme_slide_categories',
'type' => 'select',
'class' => 'hidden',
'options' => $options_categories);
I hope code I've supplied here is enough to get help. Thanks everybody
Share Improve this question asked Jan 4, 2022 at 10:02 latif tahtalatif tahta 72 bronze badges 01 Answer
Reset to default 0to 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).