wordpress - 从产品类别和发布类别查看相关产品
问题描述
我为打字道歉。我在 WordPress 的帖子类别部分中对一个词进行了分类 我在产品类别中做了完全相同的事情 没有插件或代码可以自动在帖子中显示相关的产品类别?
解决方案
WooCommerce - 显示当前产品类别的产品(查看单个产品时)
<?php
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
$query_args = array( 'post__not_in' => array( $post->ID ), 'posts_per_page' => 5, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats_array
)));
$r = new WP_Query($query_args);
if ($r->have_posts()) {
?>
<ul class="product_list_widget">
<?php while ($r->have_posts()) : $r->the_post(); global $product; ?>
<li><a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>">
<?php if (has_post_thumbnail()) the_post_thumbnail('shop_thumbnail'); else echo '<img src="'. woocommerce_placeholder_img_src() .'" alt="Placeholder" width="'.$woocommerce->get_image_size('shop_thumbnail_image_width').'" height="'.$woocommerce->get_image_size('shop_thumbnail_image_height').'" />'; ?>
<?php if ( get_the_title() ) the_title(); else the_ID(); ?>
</a> <?php echo $product->get_price_html(); ?></li>
<?php endwhile; ?>
</ul>
<?php
// Reset the global $the_post as this query will have stomped on it
wp_reset_query();
}
}
?>
推荐阅读
- c - 如何使用 libswresamples 的 swr_convert 将相同的音频转换两次
- html - 图像地图不再可点击
- indexing - 如何通过 AWS-amplify graphql 模式中的非主键查询
- apache-spark - Pyspark Dataframe 过滤条件,在 & 子句中带有布尔值和字符串
- html - 如何防止 Android 键盘在文本输入焦点上自动滚动到模式窗口的底部?
- excel - 使用 vba 根据唯一值和唯一日期对单元格求和
- python - scipy interp1d 外推法
- python - 在 Python 中访问 Gravity Forms API 的正确方法是什么?
- python - 将定义线添加到堆积面积图的最简单方法是什么?
- ios - 从 API 调用返回的 base64encoded 字符串在 Data(base64encoded: data) 运行后显示为零