Skip to main content
Add space after slug
Source Link
Dave Romsey
  • 17.9k
  • 11
  • 55
  • 70

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;>slug . ' ';
                }
            ?>
        </div>

        <?php wp_reset_postdata(); ?>

    <?php endwhile; else: ?>

        <?php //error message ?>

    <?php endif; ?>

<?php wp_reset_query(); ?>

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

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(); ?>
Reword the description of what the original code is doing.
Source Link
Dave Romsey
  • 17.9k
  • 11
  • 55
  • 70

You're gettinginitializing the $liquor_brands array, running it through a foreach loop that does nothing except for initializing $liquor_brand with the first array element of $liquor_brands, then echoing out the slug of $liquor_brand outside of the foreach loop. You$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(); ?>

You're getting the $liquor_brands, running it through a foreach loop that does nothing except for initializing $liquor_brand with the first array element of $liquor_brands, then echoing out the slug of $liquor_brand outside of the foreach loop. You should do the echoing inside of the 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(); ?>

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(); ?>
Source Link
Dave Romsey
  • 17.9k
  • 11
  • 55
  • 70

You're getting the $liquor_brands, running it through a foreach loop that does nothing except for initializing $liquor_brand with the first array element of $liquor_brands, then echoing out the slug of $liquor_brand outside of the foreach loop. You should do the echoing inside of the 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(); ?>