I have a loop built inside my custom woocommerce template single product page to output specific products based on a custom taxonomy I created. However, and for the life of me can't figure out why, but I am trying to push the "else" outside of the loop in the ul to show a custom message when there are no products to display but I keep getting an Unexpected 'else' statement error message:
<ul class="products columns-4">
<?php
$terms = get_the_terms($post->ID, 'product-lines', 'string');
$term_ids = wp_list_pluck($terms, 'term_id');
foreach ($terms as $term) {
$args = array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => array( $post->ID ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product-lines',
'field' => 'id',
'terms' => $term_ids
),
array(
'taxonomy' => 'product-types',
'field' => 'slug',
'terms' => 'moldings'
)
)
);
$product_tiles_loop = new WP_Query($args);
if ($product_tiles_loop->have_posts()) {
while ($product_tiles_loop->have_posts()) : $product_tiles_loop->the_post(); ?>
<li <?php post_class(); ?>>
<div class="product-h">
<a class="woocommerce-LoopProduct-link woocommerce-loop-product__link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="post-meta">
<h2 class="woocommerce-loop-product__title"><?php the_title(); ?></h2>
</div>
</a>
<a class="button product_type_simple" href="<?php the_permalink(); ?>" rel="nofollow">View Product</a>
</div>
</li>
<?php endwhile; } // END if have_posts loop
else {
echo 'Sorry, there are no Moldings in this Product Line';
}
wp_reset_postdata();
}
?>
</ul>
I tried moving the ul inside of the if have_posts() statement, but outside of the while statement, I get the error, like so:
<div class="product-loop-wrapper">
<?php
$terms = get_the_terms($post->ID, 'product-lines', 'string');
$term_ids = wp_list_pluck($terms, 'term_id');
foreach ($terms as $term) {
$args = array(
'post_type' => 'product',
'orderby' => 'date',
'order' => 'DESC',
'ignore_sticky_posts' => 1,
'post_status' => 'publish',
'posts_per_page' => -1,
'post__not_in' => array( $post->ID ),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product-lines',
'field' => 'id',
'terms' => $term_ids
),
array(
'taxonomy' => 'product-types',
'field' => 'slug',
'terms' => 'moldings'
)
)
);
$product_tiles_loop = new WP_Query($args);
if ($product_tiles_loop->have_posts()) { ?>
<ul class="products columns-4">
<?php while ($product_tiles_loop->have_posts()) : $product_tiles_loop->the_post(); ?>
<li <?php post_class(); ?>>
<div class="product-h">
<a class="woocommerce-LoopProduct-link woocommerce-loop-product__link" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<div class="post-meta">
<h2 class="woocommerce-loop-product__title"><?php the_title(); ?></h2>
</div>
</a>
<a class="button product_type_simple" href="<?php the_permalink(); ?>" rel="nofollow">View Product</a>
</div>
</li>
<?php endwhile; } // END if have_posts loop ?>
</ul>
<?php
else {
echo 'Sorry, there are no Moldings in this Product Line';
}
wp_reset_postdata();
}
?>
</div>
Not sure what I am missing here.