woocommerce offtopic - Else: Show Message Outside of Product Loop

admin2025-06-05  1

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.

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.

Share Improve this question asked Dec 1, 2018 at 17:36 Jason RyanJason Ryan 355 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749136078a316665.html

最新回复(0)