wordpress - Woocommerce 自定义产品循环在更新后不再工作
问题描述
我对特色产品的自定义产品循环有疑问。在更新之前它运行良好。
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) {
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice"><span style="text-decoration: line-through;">Only '.$currency.$price.'</span><span style="color: green;"> SALE '.$currency.$sale.'</span></div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
}
elseif($price)
{
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice">Only '.$currency.$price.'</div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
}
endwhile;
wp_reset_query();
?>
我已经尝试修复永久链接,但它没有解决问题。我不是 Woo 甚至 Wordpress 的专家,但这个循环确实有效。
感谢您的任何帮助。
解决方案
如果您想显示特色产品,请在查询参数中使用以下代码,它将解决问题。
<?php
$meta_query = WC()->query->get_meta_query();
$meta_query[] = array(
'key' => '_featured',
'value' => 'yes'
);
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'meta_query' => $meta_query
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$currency = get_woocommerce_currency_symbol();
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale = get_post_meta( get_the_ID(), '_sale_price', true);
if($sale) {
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice"><span style="text-decoration: line-through;">Only '.$currency.$price.'</span><span style="color: green;"> SALE '.$currency.$sale.'</span></div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
}
elseif($price)
{
echo '
<div class="pickproductsitem">
<div class="pickprodimage" style="background-image: url(' . get_the_post_thumbnail_url() . ');"></div>
<div class="prodname">'.get_the_title().'</div>
<div class="prodinfo">'. get_the_excerpt() .'</div>
<div class="prodprice">Only '.$currency.$price.'</div><a href="'.get_permalink().'" class="prodbutton w-button">VIEW</a></div>';
}
endwhile;
wp_reset_query();
?>
推荐阅读
- javascript - 为什么我的视频背景不会通过我的组件出现?
- typescript - 如何将函数声明为具有强制参数的类型
- ibm-cloud - 在 Watson 助手中保存用户输入(字符串)
- node.js - 从nodejs中的孩子更新全局变量值
- python - 永久更改 Mac 终端中的目录?(自动化无聊的东西第 6 章)
- algorithm - 生成 2 个范围内的随机非重复数字对
- amazon-web-services - AWS Cognito TOKEN 端点给出 400 Bad Request 错误“unauthorized_client”
- android-studio - Android Studio 对布局所做的更改未反映在应用程序中?
- java - 如何将菜单设置为 AppBarLayout
- javascript - Javascript 中 CSV 单元格的未定义值