0

I'm working with the WooCommerce plugin. I've got three products (called "Kits") and each kit contains several items. The "items" are a custom post type which are linked to each kit via a relationship field. Each item has a custom taxonomy attached to it ("departments"). The departments have no direct link to the products (or "kits"), except via the items.

When I display the products, I want them to show a <ul> that loops through each term found in "departments". Then within that <li>, have a nested <ul> that lists each item that is found in the current department AND in the current product.

I've got a loop that's working to display the items associated with each product:

<a href="#details<?php the_ID(); ?>" class="details button" data-id = "<?php the_ID() ?>">Details</a>
<div class="items" id="details<?php the_ID(); ?>">  
<?php  $items = get_field('kit_items');?>
<?php if( $items ): ?>
    <ul>
    <?php foreach( $items as $item ): ?>
        <?php $ident = $item->ID ?>
        <li>
            <a href="#overlay<?php echo $ident ?>" class="item item<?php echo $ident ?>" data-id ="<?php echo $ident ?>">
                <?php echo get_the_title( $item->ID ); ?>
            </a>
        </li>
    <?php endforeach; ?>
    </ul>
<?php endif; ?>

But I'm not sure how to query the custom taxonomy, print out the terms and then print the items that are associated with the department and the current product.

1

You can post the terms by using wp_get_object_terms

$terms = wp_get_object_terms($post->ID, 'departments');
if(!empty($terms)){
  foreach($terms as $term){
    $exampleName = $term->name;
    $exampleSlugs[] = $term->slug;
  }
}

Alternatively you can try to first ensure that the right taxonomy is being used:

$taxonomies = get_taxonomies();

echo '<pre>';
print_r($taxonomies);
echo '</pre>';

If everything is allright you'll see an array with all the taxonomies, and the taxonomy "departments" is present in this array.

When you want to output all the terms you can then use this:

echo '<pre>';
$departments = get_terms( 'departments', 'orderby=count&hide_empty=0' );
print_r($departments);
echo '</pre>';

Then you can use it anyway you want.

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.