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

Why does this not work?

  • Please be more elaborate in your description. Is the contents of this function not showing up? Where is it located? Are you getting PHP errors? Oct 31 '12 at 16:51
  • Thanks for editing the question. I get no errors and nothing shows up. I am adding the code to functions.php file in the currently activated theme folder. Oct 31 '12 at 17:12
  • Are you calling your function inside of category.php? Or where are do you have my_code(); being run? Oct 31 '12 at 17:13
  • im sorry i dont understand the question (im not a developer). The code is being run inside functions.php Oct 31 '12 at 17:23
  • A Function is a piece of code to be called later. so above you are defining the function my_code(), but it will never be called unless you put 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. Oct 31 '12 at 17:48

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

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.