jcadima
12/30/2019 - 9:33 PM

WP_QUERY check last post and category

<?php
https://www.isitwp.com/check-if-last-post/

$args = array(
    'taxonomy'     => 'product_cat',
    'orderby'      => 'name',
    'show_count'   => 0,
    'pad_counts'   => 0,
    'hierarchical' => 0,
    'title_li'     => '',
    'hide_empty'   => 1
);

$all_categories = get_categories( $args );
?>


<!--  ################ TAB NAVIGATION #################   -->
<ul class="nav nav-tabs text-center">
    <li class="img__wrap active">
        <a href="#latest" data-toggle="tab">
            <small>NEW ARRIVALS</small>
        </a>
    </li>

    <li class="img__wrap">
        <a href="#collections" data-toggle="tab">
            <small>COLLECTIONS</small>
        </a>
    </li>

    <li class="img__wrap">
        <a href="#info" data-toggle="tab">
            <small>WHY ANZZI</small>
        </a>
    </li>
</ul>

<div class="tab-content text-center">

    <!--  ------------------ LATEST PRODUCTS ------------------- -->
<div class="tab-pane active" id="latest">
    <div class="row">
        <!--  ################ TAB NAVIGATION #################   -->
        <ul class="nav nav-tabs text-center">
            <?php
            $i = 0 ;
            foreach ($all_categories as $cat) {
                if($cat->category_parent == 0) { // if parent category display the category
                    $category_id = $cat->term_id;
                    $cat_thumb_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
                    $cat_thumb_url = wp_get_attachment_thumb_url( $cat_thumb_id );
                    // category thumbnail url
                    $cat_image = ( $cat_thumb_url) ? $cat_thumb_url : '/wp-content/themes/anzzi/images/placeholder.jpg' ;
                    ?>
                    <li class="img__wrap <?php echo  ($i == 0) ? 'active' : '' ; ?>">

                        <img class="img__img" src="<?php echo $cat_image; ?>" />
                        <div class="img__description_layer">
                            <a href="#<?php echo $cat->slug ; ?>" data-toggle="tab">
                                <small><?php echo $cat->name;  ?></small>
                            </a>
                        </div>
                    </li>
                    <?php
                    $i++;
                }
            }
            wp_reset_query();
            ?>
        </ul>

        <!-- #############  TAB CONTENT ############### -->
        <div class="tab-content text-center">
            <?php
            $j = 0 ;
            foreach( $all_categories as $cat ) :
            if($cat->category_parent == 0) {
            $category_id = $cat->term_id;

            $latestproducts = new WP_Query(array(
                'post_type' => 'product',
                'post_status' => 'publish',
                'posts_per_page' => 5,
                'tax_query' => array(array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => array($category_id),
                ))
            ));
            ?>
            <div class="tab-pane <?php echo ($j == 0) ? 'active' : ''; ?>" id="<?php echo $cat->slug; ?>">
                <div class="row">
                    <?php
                    $k = 0;
                    while ($latestproducts->have_posts()) : $latestproducts->the_post(); ?>

                    <?php if( $k == 0 ) :  // MAIN PRODUCT

                    $thumb_id = get_post_thumbnail_id();
                    $product_image = ( $thumb_id ) ? $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true)[0] : '/wp-content/themes/anzzi/images/placeholder.jpg' ;
                    ?>
                    <div class="col-md-6 main-product <?php echo 'post-'. $k ?>" style="background-image: url(<?php echo $product_image ; ?>);">

                        <div class="blurb">
                            <h2><?php echo 'NEW ' . strtoupper($cat->name) ; ?></h2>
                            <p>Blurb Text</p>
                            <p>
                                <a href="<?php echo get_permalink() ; ?>" class="btn btn-transparent">VIEW ALL</a>
                            </p>
                            <p>
                                <a href="<?php echo get_permalink() ; ?>">
                                    <span><?php echo get_the_title(); ?></span></a>
                            </p>
                        </div>
                    </div> <!-- END main-product  -->

                    <div class="col-md-6 product-container"> <!-- START RIGHT COL  -->

                        <?php $k++; continue; // update index and exit, runs one time only for main product layout
                        endif; ?>

                        <div class="col-md-6 product-item <?php echo 'post-' . $k ; ?>">
                            <?php
                            $thumb_id = get_post_thumbnail_id();
                            $product_image = ( $thumb_id ) ? $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true)[0] : '/wp-content/themes/anzzi/images/placeholder.jpg' ;
                            ?>
                            <figure>
                                <img class="img-responsive" src="<?php echo $product_image  ; ?>">
                                <figcaption>
                                    <a href="<?php echo get_permalink() ; ?>"><?php echo get_the_title(); ?></a>
                                </figcaption>
                            </figure>

                        </div>
                        <?php  if(  ( $latestproducts->current_post + 1 ) == ( $latestproducts->post_count ) )  echo '</div><!-- CLOSE PRODUCT-ITEM -->' ;  ?>

                        <?php $k++; // update index for second phase
                        endwhile;
                        $k = 0;
                        ?>

                    </div> <!-- END row -->
                </div> <!--  END tab-pane  -->


                <?php
                wp_reset_query();
                $j++;
                }
                endforeach; ?>
            </div>
        </div> <!-- END row -->
    </div> <!--  END tab-pane  -->


    <!-- ------------------  COLLECTIONS ----------------------- -->
    <div class="tab-pane" id="collections">
        <div class="row">

            <!--  ################ TAB NAVIGATION #################   -->
            <ul class="nav nav-tabs text-center">
                <?php
                $i = 0 ;
                foreach ($all_categories as $cat) {
                    if($cat->category_parent == 0) { // if parent category display the category
                        ?>

                        <div class="">
                            <a href="#<?php echo $cat->slug ; ?>_collection" data-toggle="tab">
                                <small><?php echo $cat->name;  ?></small>
                            </a>
                        </div>

                        <?php
                        $i++;
                    }
                }
               //  wp_reset_query();
                ?>
            </ul>

            <!-- #############  TAB CONTENT ############### -->
            <div class="tab-content text-center">
                <?php
                $j = 0 ;
                foreach( $all_categories as $cat ) :

                    if($cat->category_parent == 0) {
                      //  $category_id = $cat->term_id;
                        $args = array(
                            'number'   => 5,
                            'hierarchical'     => 1,
                            'show_option_none' => '',
                            'hide_empty'       => 1,
                            'parent'           => $cat->term_id,
                            'taxonomy'         => 'product_cat'
                        );
                        $subcats = get_categories($args) ;
//                            echo 'category count: ' . count($subcats) . '<br>' ;
                    ?>

                <div class="tab-pane <?php echo ($j == 0) ? 'active' : ''; ?>" id="<?php echo $cat->slug; ?>_collection">
                    <div class="row">
                        <?php
                        $k = 0;
                        foreach ( $subcats as $subcat ) : ?>

                        <?php if( $k == 0 ) :  // MAIN PRODUCT

                        $thumb_id = get_woocommerce_term_meta( $subcat->term_id, 'thumbnail_id', true );
                        $collection_image = ( $thumb_id ) ? $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'full', true)[0] : '/wp-content/themes/anzzi/images/placeholder.jpg' ;
                        ?>
                        <div class="col-md-6 main-product <?php echo 'collection-'. $k ?>" style="background-image: url(<?php echo $collection_image ; ?>);">

                            <div class="blurb">
                                <h2><?php echo strtoupper($cat->name) ; ?></h2>
                                <p>Blurb Text</p>
                                <p>
                                    <a href="<?php echo get_term_link( $subcat->slug, $subcat->taxonomy ) ; ?>"><span>SHOP NOW</span></a>
                                </p>
                            </div>
                        </div> <!-- END main-product  -->

                        <div class="col-md-6 product-container"> <!-- START RIGHT COL  -->

                            <?php $k++; continue; // update index and exit, runs one time only for main collection layout
                            endif; ?>

                            <div class="col-md-6 product-item <?php echo 'collection-' . $k ; ?>">
                                <?php
                                $thumb_id = get_post_thumbnail_id();
                                $collection_image = ( $thumb_id ) ? $thumb_url_array = wp_get_attachment_image_src($thumb_id, 'medium', true)[0] : '/wp-content/themes/anzzi/images/placeholder.jpg' ;
                                ?>
                                <figure>
                                    <img class="img-responsive" src="<?php echo $collection_image  ; ?>">
                                    <figcaption>
                                        <a href="<?php echo get_term_link( $subcat->slug, $subcat->taxonomy ) ; ?>"><span><?php echo $subcat->name;  ?></span></a>
                                    </figcaption>
                                </figure>

                            </div>
                            <?php  if(  $k  ==  count($subcats) - 1 )  echo '</div><!-- CLOSE COLLECTION-ITEM -->' ;  ?>

                            <?php $k++; // update index for second phase
                            endforeach;
                            $k = 0;
                            ?>

                        </div> <!-- END  Inner row -->
                    </div> <!--  END tab-pane  -->


                    <?php
                   // wp_reset_query();
                    $j++;
                    }
                    endforeach; ?>
                </div> <!--  END tab content COLLECTION -->
        </div> <!-- END row  COLLECTION-->
    </div> <!--  END tab-pane  COLLECTION -->


    <!--  ---------------------------- WHY ANZZI ------------------------   -->
    <div class="tab-pane" id="info">
        <div class="row">
            <?php if( have_rows('why_anzzi_blocks', 'options') ): ?>

                <?php while( have_rows('why_anzzi_blocks', 'options') ): the_row();
                    // vars
                    $image   = get_sub_field('why_anzzi_image', 'options');
                    $title   = get_sub_field('why_anzzi_title', 'options');
                    $content = get_sub_field('why_anzzi_content', 'options');
                ?>

                <div class="col-md-4">
                   <div class="info_image">
                       <img class="img-responsive" src="<?php echo $image; ?>">
                   </div>

                    <div class="info_title">
                        <?php echo $title; ?>
                    </div>

                    <div class="info_content">
                        <p><?php echo $content; ?></p>
                    </div>
                </div>

                    <?php endwhile; ?>

            <?php endif; ?>

        </div> <!-- END row -->
    </div> <!--  END tab-pane  -->

</div>