4

Been trying to figure out how I can get the product attributes of the current product and store it in a variable and put it in a class.

I've managed to get the product attributes, unfortunately it seems to be displaying all the products attributes to all the products I have set. Here is the code I have been working.

    <div id="Container" class="nine columns mixitup-container bevtools-liquor">

        <?php

        $liquor = new WP_Query( array( 
            'post_type'   => 'product',
            'product_cat' => 'liquors',
            'meta_query'  => array(
                array(
                    'key'   => '_stock_status',
                    'value' => 'instock'
                )
            )
        ) );

        if ( $liquor->have_posts() ) : while ( $liquor->have_posts() ) : $liquor->the_post();
        ?>


        //In this foreach loop, I'm trying to get all the terms for liquor-brands attributes

        <?php 
        $brand_terms = get_the_terms( $post, 'pa_liquor-brands' );
        foreach ( $brand_terms as $term ) :
        ?>

        <?php $brand_string = ''; ?>
        <?php $brand_string .= $term->slug . ' '; ?>

        <?php endforeach; ?>



        <div id="post-<?php the_ID(); ?>" class="three columns mix product-post <?php echo $brand_string  ?>" >
        </div>


        <?php wp_reset_postdata(); ?>

        <?php endwhile; else: ?>

        <?php //error message ?>

        <?php endif; ?>


        <?php wp_reset_query(); ?>

After running the code here is what the output looks like.

<div id="post-2190" class="34th-pursuit-joes-brew absolut aviation-gin bacardi botanist citadelle-gin don-papa gvine grey-goose jack-daniel johnnie-walker makers-mark monkey-shoulder pale-ale-katipunan" style="display: inline-block;" data-bound="">
</div>

<div id="post-2192" class="34th-pursuit-joes-brew absolut aviation-gin bacardi botanist citadelle-gin don-papa gvine grey-goose jack-daniel johnnie-walker makers-mark monkey-shoulder pale-ale-katipunan" style="display: inline-block;" data-bound="">
</div>

As you can see both products are showing all the product attributes instead of showing what is assigned to them instead.

1 Answer 1

6

get_terms() Retrieves the terms in a given taxonomy or list of taxonomies.

What you need is

get_the_terms() Retrieves the terms of the taxonomy that are attached to the post.

So you can simply replace

$brand_terms = get_terms( 'pa_liquor-brands' );

with

$brand_terms = get_the_terms( $post, 'pa_liquor-brands' );

And that should to the trick.

You can read more about these two functions here:

https://developer.wordpress.org/reference/functions/get_terms/ https://developer.wordpress.org/reference/functions/get_the_terms/

Edit: And you will also need to reset your $brand_string otherwise it will add terms from another posts and output them

$brand_terms = get_the_terms($post, 'pa_liquor-brands');
$brand_string = ''; // Reset string
foreach ($brand_terms as $term) :
    $brand_string .= $term->slug . ' ';
endforeach;

// echo $brand_string down here somewhere
6
  • I tried your solution but it still shows the same problem. It still shows all the taxonomies of all the products
    – clestcruz
    Sep 27, 2016 at 15:07
  • Try resetting $brand_string just inside your loop. If you don't reset it then it will accumulate data from other posts.
    – ngearing
    Sep 27, 2016 at 23:14
  • Sorry but how do you reset the $brand_string inside the loop
    – clestcruz
    Sep 28, 2016 at 0:09
  • $brand_string = '';
    – ngearing
    Sep 28, 2016 at 0:10
  • Hello Nath, I've updated my code with your answer. Not sure if I did right, you can check above
    – clestcruz
    Sep 28, 2016 at 0:48

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.