我想将一些自定义字段项注入woocommerce产品存档页面(基于类别)。一个例子是:在所有项链的档案页面上,我想要显示项链的链接网格以及一些支持图像(模特戴项链等),这些图像可以在阵列中的随机点注入但是它们不会必须是链接。
我已经成功地提取了我想要的图像,尽管它们目前位于一个单独的数组中,并且在循环之前被分组在页面的顶部。那么如何合并woocommerce产品循环数组和我的自定义图像数组?
我抓住了我archive-product.php
顶部设置的自定义字段:
// Get ACF Image Repeater
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$modelimages = get_field('model_images', $taxonomy . '_' . $term_id);
// If there are model images for the this product category
if($modelimages):
// Create empty model image array
$models = array();
while( have_rows('model_images', $taxonomy . '_' . $term_id) ): the_row();
// Add URLs of model pix to the array
$models[] = get_sub_field('file')['sizes']['medium'];
endwhile;
endif;
上面只是创建了一个图像网址数组,工作正常。我想注射'如果这些项目随机分为archive-product.php
<?php woocommerce_product_loop_start(); ?>
<?php woocommerce_product_subcategories(); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
那么如何创建一个结合产品和非产品图像的输出循环或数组,例如:
产品1,
产品2,
非产品(图像),
产品3,
产品4,
产品5,
非产品(图像)
产品6
等....
如果有一个插件,任何人都知道已经实现了这个,那就太棒了。
- 编辑---
我以某种方式让这个工作。不确定它是最好的解决方案,但万一它可以帮助其他人:在archive-product.php
我用以下内容替换了循环
// I have set an Advanced Custom Fields Repeater File(model_images) for product categories
// Get current archive category
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// Get related product category images from ACF
$modelimages = get_field('model_images', $taxonomy . '_' . $term_id);
// Create empty models array
$models = array();
// If there are model images
if($modelimages):
while( have_rows('model_images', $taxonomy . '_' . $term_id) ): the_row();
// Add URLs of model image files to the array
$models[] = get_sub_field('image')['sizes']['medium'];
endwhile;
endif;
if ( get_query_var( 'taxonomy' ) ) { // If on a product taxonomy archive (category or tag)
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, //all
'paged' => get_query_var( 'paged' ),
'tax_query' => array(
array(
'taxonomy' => get_query_var( 'taxonomy' ),
'field' => 'slug',
'terms' => get_query_var( 'term' ),
),
),
);
}
// make an array of the products
$products = get_posts( $args );
// merge the arrays of products and models
$merge = array_merge($products, $models);
// shuffle the merged array
$keys = array_keys($merge);
shuffle($keys);
$random = array();
foreach ($keys as $key) {
$random[$key] = $merge[$key];
};
foreach($random as $r) {
if($r->post_type !== 'product') :
// do layout stuff with the model picture
else :
// it's a product
$args = array(
'post_type' => 'product',
'p' => $r->ID
);
$product_query = new WP_Query( $args );
if ( $product_query->have_posts() ) :
while ( $product_query->have_posts() ) : $product_query->the_post();
// use the woocommerce template for products
wc_get_template_part( 'content', 'product' );
endwhile;
wp_reset_postdata();
endif;
endif;
endforeach;
它可能是各种丑陋的,但至少它起作用了。