Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Woocommerce get category id when o the category pages

HI All,

I have a slider on my category template file that shows products and this works fine.

The issue i have is that i only want to display the products from the category that the user is one. The plugin allows me to drop in the categories id.

what is the easiest way of putting the categories id into a variable?

i am wanting to put this into the template page so would need to be able to work for any category.

Cheers

Brett

2 Answers

Chris Hubbard
Chris Hubbard
2,253 Points

It's hard to say without seeing your working code, but you could try something like this:

$cat = get_the_category();

$args = array(
    'post_type' => 'post',
    'category_name' => '$cat',
);

$the_query = new WP_Query( '$args' );

if ( have_posts() ) : while ( $the_query -> have_posts() ) : $the_query -> the_post();
Matt Campbell
Matt Campbell
9,767 Points

With woocommerce it's product_cat().