Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  Eric Holmes 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. –  user1768787 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? –  Eric Holmes 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 –  user1768787 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. –  Eric Holmes Oct 31 '12 at 17:48

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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