0

I'm trying to display the attributes of the current product. In the screenshot below, I declared the attribute "Liquor Brands" and assigned multiple values to it:

enter image description here

Here is the code I'm currently working on:

        <?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();

        ?>

        <?php 

        $liquor_brands = get_terms('pa_liquor-brands');
        foreach ( $liquor_brands as $liquor_brand ) :
        ?>

        <?php endforeach; ?>



        <div id="post-<?php the_ID(); ?>"  class="three columns product-post">

        <?php echo $liquor_brand->slug ?>

        </div>



        <?php wp_reset_postdata(); ?>

        <?php endwhile; else: ?>

        <?php //error message ?>

        <?php endif; ?>


        <?php wp_reset_query(); ?>

Here is the screenshot of the output. It only shows the last value that I have set, which is "very-old-captain-rum":

enter image description here

1 Answer 1

0

You're initializing the $liquor_brands array, running it through a foreach loop that does nothing, then echoing out the slug of $liquor_brand outside of the foreach loop. $liquor_brand is going to be set to the last item in the $liquor_brands array since the whole array was iterated over. Long story short, you should do the echoing inside of the foreach loop:

<?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(); ?>

        <div id="post-<?php the_ID(); ?>"  class="three columns product-post">
            <?php 
                $liquor_brands = get_terms( 'pa_liquor-brands' );
                foreach ( $liquor_brands as $liquor_brand ) {
                    echo $liquor_brand->slug . ' ';
                }
            ?>
        </div>

        <?php wp_reset_postdata(); ?>

    <?php endwhile; else: ?>

        <?php //error message ?>

    <?php endif; ?>

<?php wp_reset_query(); ?>
3
  • I think you actually want to use $liquor_brand->name. The slug is not supposed to have spaces. Check out the documentation (particularly the examples at the bottom) for details on the available properties. Commented Sep 15, 2016 at 1:35
  • I see, so there is no way to add spaces between the slugs.
    – clestcruz
    Commented Sep 15, 2016 at 1:38
  • I misunderstood. Yes, you can just add a space after each one. Updated the code. Commented Sep 15, 2016 at 1:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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