• Resolved FrenchFries

    (@frenchfries)


    Hi,

    I am trying to get a list of all product categories into an array for something on my main page, but none of what I have tried is working – the array is always empty with zero elements.

    These return an empty array:
    get_terms(‘product_cat’);
    get_categories(array(‘taxonomy’ => ‘product_cat’));

    But if I change the taxonomy like so:
    get_terms(‘product_type’);

    Then the product_type taxonomy terms are listed.

    I did notice in the MySQL database that all rows with taxonomy of ‘product_cat’ in the term_taxonomy table have the value zero in the count column, which might give some explanation, but in all other respects the site is functioning correctly – when I navigate to a category the correct products are listed.

    So I am bit stuck and would like some help and ideas.

    Thanks,
    Paul

    https://wordpress.org/plugins/jigoshop/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi Paul,

    If you would like to show categories try use the code below, put it in single-product.php file

    <?php
    $terms = get_the_terms( $post->ID, ‘product_cat’ );
    echo ‘

    ‘;
    ?>

    Paul,

    There was a problem posting the code, please follow the link below:

    Categories List

    Thread Starter FrenchFries

    (@frenchfries)

    Hello,

    Thanks for your suggestion, but I am not trying to display the categories attached to a product, I am trying to show all categories at the home page level.

    I can see that at <mysitename>/shop that all the categories are listed at the top of the page, but I cannot work out how to replicate this myself.

    Thanks,
    Paul

    Thread Starter FrenchFries

    (@frenchfries)

    Hello,

    I have resolved the problem and now

    get_categories(array('taxonomy' => 'product_cat'));

    does return all my categories.

    I updated the ‘term_taxonomy’ MySQL table and set the count column value to 1 for all rows with a taxonomy of ‘product_cat’.

    I should mention that I am using SQL Server Integration Services to load the shop data into WordPress and Jigoshop, I am not really using the Jigoshop UI to manage the shop data.

    Thanks,
    Paul

    The reason your original call did not return the terms was because there is an argument to the get_terms() function, ‘hide_empty’. Since your terms were not assigned to any posts (products), they were not returned.

    This would work:
    $terms = get_terms('product_cat', array('hide_empty' => false));

    That would be better than manually updating the counts first (which works because then they appear to not be empty.)

    I found your resolution when having the same problem and it made me realize what was going wrong, so hopefully this will help the next person finding this topic.

    Hi Paul, AaronOverton,

    Thanks for your efforts, we do appreciate it.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘List all Product Categories’ is closed to new replies.