我在自定义woocommerce模板的单个产品页面中构建了一个循环,以根据我创建的自定义分类法输出特定的产品。然而,我一直不明白为什么,但我正试图将“else”推到ul循环之外,以便在没有产品可显示时显示自定义消息,但我不断收到意外的“else”语句错误消息:
<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>
我尝试将ul移动到if have\\u posts()语句的内部,但在while语句的外部,我得到了错误,如下所示:
<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>
不知道我在这里错过了什么。
最合适的回答,由SO网友:butlerblog 整理而成
您似乎有一些混合语法。
这个endwhile;
不需要右大括号。我想你的大括号正在关闭else
之后会出现这种情况,但因为您关闭并重新打开PHP以将</ul>
标记,它会导致语法错误。
请这样尝试(注意大括号从endwhile行移到else行的位置):
<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>