最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

options - display category name based on its equivalent id

programmeradmin3浏览0评论

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 0
Add a comment  | 

1 Answer 1

Reset to default 0

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).

发布评论

评论列表(0)

  1. 暂无评论