0

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.

1 Answer 1

1

It appears that you have some mixed syntax.

The endwhile; does not need a closing curly brace. I think that the curly brace you have there is closing the else that comes later, but because you close and reopen PHP to put in the </ul> tag, it causes a syntax error.

Try it like this (note where the curly brace was moved from the endwhile line to the else line):

<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>
1
  • No problem - glad to hear that was it!
    – butlerblog
    Commented Dec 1, 2018 at 20:08

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.