On a woocommerce site, I am trying to insert a piece of code on all category pages. The code I am using is:
function my_code() {
if ( ! is_tax('product_cat') ) return;
?>
<!-- my code goes here-->
<?php
}
Why does this not work?
The is_tax() function checks if a custom taxonomy archive page is being displayed. So it will only work in template files such as taxonomy-product_cat.php - unless you create new templates for specific categories in the product_cat taxonomy. For example, if you have a product category called "Toasters" and the slug is "toasters" then the template file would be taxonomy-product_cat-toasters.php, and if that doesn't exist then the taxonomy-product_cat.php template is loaded instead.
See this page for template hierarchy: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
category.php
? Or where are do you havemy_code();
being run?functions.php
my_code();
somewhere. In the same folder as functions.php, you should see a category.php file. Put <?php my_code(); ?> where you want the output of this function to appear.