Custom loop – Isolating post meta output depending on current query taxonomy terms

I managed to sort the problem out myself with a fresh set of eyes but thought it would be useful for others if they find themselves with a similar issue in future. If the answer needs tidying please feel free to suggest any edits.

I had to edit a few lines of code in order to resolve it but here is the complete block again.

Firstly I had to add in an additional variable to store the current product taxonomy term inside another variable by adding in the below inside of the post output.

$currentslug = $currentproduct->slug

I then proceeded to change the call

if ( $currentproduct->name="bottle" ) {

To this noting to add the double “=” (means $currentslug and 'bottle' are equal) which meant the variable $currentslug would be checked and reset on each run through the loop instead of checking $currentproduct->name on the fly. Someone please correct me if I have misunderstood this but I’m assuming $currentproduct->name doesnt work because it hasn’t been stored anywhere for use later in the loop?

<?php if ($currentslug == 'bottle') {

Which allowed me to identify the isolate the current taxonomy term to go in and add in an additional call to pull through the correct attachment thumbnail from the relevant meta field.

Which I did via

<?php 
 if (!empty($bottle)) {
        $attachment_id = $bottle[id];
    } else {
        $attachment_id = NULL;
    }
    $bottlethumb = wp_get_attachment_image( $attachment_id, 'medium' ); 
    echo $bottlethumb 
 ?>

And finally the working code (there are still a few bits I need to add in but the initial problem has been resolved).

    <?php $beertax_terms = get_terms( 'beer_tax' ); ?>
<?php foreach($beertax_terms as $beertax_term) { ?>
<?php $currentproduct = $beertax_term ?>
<div class="beerproducts">
    <h3><?php echo ($beertax_term->name) ?></h3>
        <?php $beerbrewer_terms = get_terms( 'beer_brewery' ); ?>
            <?php foreach($beerbrewer_terms as $beerbrewer_term) { ?>

                <div class="beerbrewery">
                <h4><?php echo ($beerbrewer_term->name) ?></h4>
                    <?php $beerargs = array (
                            'post_type' =>  'beer',
                            'orderby'   =>  'name',
                            'order'     =>  'ASC',
                            'tax_query' => array(
                                'relation' => 'AND',
                                array(
                                    'taxonomy' => 'beer_tax',
                                    'field'    => 'slug',
                                    'terms'    => ($beertax_term->slug),
                                ),
                                array(
                                    'taxonomy' => 'beer_brewery',
                                    'field'    => 'slug',
                                    'terms'    => ($beerbrewer_term->slug),
                                ),
                            ),
                        ); ?>
                    <?php $the_query = new WP_Query( $beerargs ); ?>
                        <?php if ($the_query->have_posts()) { ?>
                            <?php while ($the_query->have_posts()) { ?>
                                <?php $the_query->the_post(); ?>

                                <?php $cask = get_post_meta($post->ID, 'beer_caskthumb', true); ?>
                                <?php $keg = get_post_meta($post->ID, 'beer_kegthumb', true); ?>
                                <?php $bottle = get_post_meta($post->ID, 'beer_bottlethumb', true); ?>
                                <?php $currentslug = $currentproduct->slug ?>
                                    <a href="https://wordpress.stackexchange.com/questions/192050/<?php the_permalink();?>" rel="bookmark">
                                        <div class="beer">

                                            <?php if ($currentslug == 'bottle') { ?>
                                                <!--bottle-->
                                                <?php if (!empty($bottle)) {
                                                            $attachment_id = $bottle[id];
                                                        } else {
                                                            $attachment_id = NULL;
                                                            }
                                                        $bottlethumb = wp_get_attachment_image( $attachment_id, 'medium' ); 
                                                        echo $bottlethumb ?>

                                            <?php } if ($currentslug == 'cask') { ?>
                                                <!--cask-->
                                                <?php if (!empty($bottle)) {
                                                            $attachment_id = $cask[id];
                                                        } else {
                                                            $attachment_id = NULL;
                                                            }
                                                        $caskthumb = wp_get_attachment_image( $attachment_id, 'medium' ); 
                                                        echo $caskthumb ?>

                                            <?php } if ($currentslug == 'keg') { ?>
                                                <!--keg-->
                                                <?php if (!empty($keg)) {
                                                            $attachment_id = $keg[id];
                                                        } else {
                                                            $attachment_id = NULL;
                                                            }
                                                        $kegthumb = wp_get_attachment_image( $attachment_id, 'medium' ); 
                                                        echo $kegthumb ?>
                                            <?php } ?>    

                                        </div>
                                    </a>

                            <?php } ?>
                            <?php wp_reset_query(); wp_reset_postdata();?>
                        <? } ?>
                        <?php wp_reset_postdata(); ?>
                <!--close beerbrewer-->
                </div>
            <?php } ?>
            <?php wp_reset_query(); ?>
<!--close beerproducts-->
</div>
<?php } ?>

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)