$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - Else: Show Message Outside of Product Loop|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - Else: Show Message Outside of Product Loop

matteradmin10PV0评论

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>
Post a comment

comment list (0)

  1. No comments so far