I have a wordpress website with woocommerce.

I have the need to link to the parent category. The use-case for this would be - if there is a product that is available in different sizes, show a link to the parent category so that they can "click here to view more".

My PHP is a little rusty to say the least, I'm mainly front-end, but here's what I need some code to do.

I'm hoping to place this within the product template file, or would it be best in functions.php? I'm not sure...

My best attempt at psuedo code:

If this product has a parent category
AND this parent category has more than one product in it
echo a link to that category with the text "Click here for more sizes"

Can anyone show me how to code that and where best to place it? I'm using the storefront theme.

1 Answer 1


There are two files that WooCommerce pulls from to create a product page: single-product.php and content-single-product.php.

Single-product.php determines what information is displayed on the page and creates the layout that will display the information. You can view its content here.

Content-single-product.php pulls the content of the product that’s been organized into hooks with actions and filters, and nests those into the single-product.php layout. You can view its content here.

In order to make sure that WooCommerce doesn't erase any customizations that you make, you'll need to make copies of each of these files and put them in a /woocommerce folder in your child theme.

You will basically have to use content-single-product.php and add your code there, I don't really know where do you want to display it, so I cannot tell where.

In order to check if a product has Parent Category, you could use this answer, which returns the parent categories, so in your case, if the return is an empty array you don't have parent categories, but if it's not empty use it on your echo.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.