0

So I have a super simple Woocommerce shortcode [product_categories], It's supposed to show product categories from the shop.

It does this job perfectly if I put it inside the contents of a page as such: [product_categories].

However, if I put the same thing into the template directly it suddenly returns the first post of current category? What?!

So in my template I have only these two rows:

<?php echo do_shortcode('[product categories]'); // this returns a single product which is wrong and totally weird ?>
<?php the_content(); // this returns the categories as intended ?>

I never knew these two methods have some kind of difference. How can I make the shortcode work with the echo way?

5
  • 1
    You are missing the "_" in your shortcode. It should be [product_category] or [product_categories]. Please check if this is the problem here. Example from the official WooCommerce shortcode documentation: [product_categories number="0" parent="0"]
    – LWS-Mo
    Oct 28, 2019 at 13:22
  • @LWS-Mo If [product_categories] is working, it should work here as well, the other stuff are filter / limit parameters. but good point here : "_" jussi if you've copied the code directly from the template file, please recheck if that's the case as you've mentioned that it works but shows only one product. Oct 28, 2019 at 13:33
  • 1
    Yes [product_categories] is working. But if the shortcode [product categories] is used, this will show a single product. Because [product] is just a shortcode to show products. To show the product categories itself [product_categories] should be used. It seems that @Jussi has just forgot the underscore in his shortcode.
    – LWS-Mo
    Oct 28, 2019 at 13:38
  • Omg... feeling stupid after the answer is so humiliatingly easy is never getting old for me. Yes, the problem was the lack of underscore for christs sake. To think I have the balls to consider myself a WP developer.
    – Jussi
    Oct 28, 2019 at 13:45
  • it was like missing a ; while coding, i recommend you should always copy the shortcode from the available shortcode list to avoid these minor mistakes... Oct 28, 2019 at 13:53

1 Answer 1

0

I was lacking the underscore in [product categories].

<?php echo do_shortcode('[product_categories]'); ?>

Above code with "_" returns the correct answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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