按分类法在自定义 Post 类型中重复 Post
Duplicate Posts In Custom Post Type by Taxonomy
我有一个名为 Products 的自定义 Post 类型(不是 WooCommerce)。用户使用 ACF 分类法字段选择类别。我正在尝试获取每个选定类别的每个产品,但有些产品属于多个类别,因此当我遍历行时,它们会打印多次。
<?php
$product_categories = get_field( 'product_filter_categories' );
$custom_taxonomy='product_category';
$custom_terms = $product_categories;
foreach($custom_terms as $custom_term) :
wp_reset_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
$product_coming_soon_image = get_field( 'product_coming_soon_image' );
while($loop->have_posts()) : $loop->the_post();
$termsArray = get_the_terms( $post->ID, "product_category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
} ?>
<div class="<?php echo $termsString; ?> isotope-item">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php the_post_thumbnail_url('') ?>" class="lazy" alt="<?php the_title() ?>" loading="lazy" width="240" height="240">
<?php else : ?>
<img src="<?php echo esc_url( $product_coming_soon_image['sizes']['our-products'] ); ?>" alt="<?php echo esc_attr( $product_coming_soon_image['alt'] ); ?>" loading="lazy" width="240" height="240" />
<?php endif; ?>
</a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php
endwhile; ?>
<?php
endif;
endforeach; ?>
</div>
我无法确定重复项的来源。
$product_categories = get_field( 'product_filter_categories' );
$custom_taxonomy='product_category';
$custom_terms = $product_categories->term_id;
foreach($custom_terms as $custom_term) :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'term_id',
'terms' => array($custom_terms),
'operator' => 'IN',
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
$product_coming_soon_image = get_field( 'product_coming_soon_image',get_the_ID());
while($loop->have_posts()) : $loop->the_post();
$termsArray = get_the_terms( get_the_ID(), "product_category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
} ?>
<div class="<?php echo $termsString; ?> isotope-item">
<a href="<?php get_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php the_post_thumbnail_url('') ?>" class="lazy" alt="<?php get_the_title() ?>" loading="lazy" width="240" height="240">
<?php else : ?>
<img src="<?php echo esc_url( $product_coming_soon_image['sizes']['our-products'] ); ?>" alt="<?php echo esc_attr( $product_coming_soon_image['alt'] ); ?>" loading="lazy" width="240" height="240" />
<?php endif; ?>
</a>
<h3><a href="<?php echo get_permalink()(); ?>"><?php get_the_title(); ?></a></h3>
</div>
<?php
endwhile; wp_reset_query();?>
<?php
endif;
endforeach; ?>
</div>
好的,所以你正在做的是:
- 获得一些条件
- 按条件循环
- 按期限获取产品
- 循环显示产品
确实如此,如果一个产品与循环中的多个术语相关联,它们将以这种方式出现多次。
您应该删除第一个 foreach,并通过多个术语查询产品,然后在 wp_query 结果上循环。这样你就不会重复:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => array_column($custom_term, 'slug'),
),
),
);
如果您确实需要根据您的条件进行循环,并在每个循环中查询产品:您需要将所有查询的 post ID 存储在一个数组中,该数组是在循环外启动的。并将此 ID 数组“已查询”添加到查询“post not in: ids”。
我能够通过在第一个 foreach 之前创建一个空数组然后在循环时检查当前 post ID 是否在该数组中来删除重复项。如果当前 post id 不在数组中,将其添加到数组并显示产品。如果当前postid在数组中,则跳过。
更新代码
$product_categories = get_field( 'product_filter_categories' );
$custom_taxonomy = 'product_category';
$custom_terms = $product_categories;
$displayed_products = array();
foreach($custom_terms as $custom_term) :
wp_reset_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
$product_coming_soon_image = get_field( 'product_coming_soon_image' );
while($loop->have_posts()) : $loop->the_post();
if(!in_array( $post->ID, $displayed_products )) :
$termsArray = get_the_terms( $post->ID, "product_category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
} ?>
<div class="<?php echo $termsString; ?> isotope-item">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php the_post_thumbnail_url('') ?>" class="lazy" alt="<?php the_title() ?>" loading="lazy" width="240" height="240">
<?php else : ?>
<img src="<?php echo esc_url( $product_coming_soon_image['sizes']['our-products'] ); ?>" alt="<?php echo esc_attr( $product_coming_soon_image['alt'] ); ?>" loading="lazy" width="240" height="240" />
<?php endif; ?>
</a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php
array_push( $displayed_products, $post->ID );
endif;
endwhile; ?>
<?php
endif;
endforeach; ?>
'''
我有一个名为 Products 的自定义 Post 类型(不是 WooCommerce)。用户使用 ACF 分类法字段选择类别。我正在尝试获取每个选定类别的每个产品,但有些产品属于多个类别,因此当我遍历行时,它们会打印多次。
<?php
$product_categories = get_field( 'product_filter_categories' );
$custom_taxonomy='product_category';
$custom_terms = $product_categories;
foreach($custom_terms as $custom_term) :
wp_reset_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
$product_coming_soon_image = get_field( 'product_coming_soon_image' );
while($loop->have_posts()) : $loop->the_post();
$termsArray = get_the_terms( $post->ID, "product_category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
} ?>
<div class="<?php echo $termsString; ?> isotope-item">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php the_post_thumbnail_url('') ?>" class="lazy" alt="<?php the_title() ?>" loading="lazy" width="240" height="240">
<?php else : ?>
<img src="<?php echo esc_url( $product_coming_soon_image['sizes']['our-products'] ); ?>" alt="<?php echo esc_attr( $product_coming_soon_image['alt'] ); ?>" loading="lazy" width="240" height="240" />
<?php endif; ?>
</a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php
endwhile; ?>
<?php
endif;
endforeach; ?>
</div>
我无法确定重复项的来源。
$product_categories = get_field( 'product_filter_categories' );
$custom_taxonomy='product_category';
$custom_terms = $product_categories->term_id;
foreach($custom_terms as $custom_term) :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'term_id',
'terms' => array($custom_terms),
'operator' => 'IN',
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
$product_coming_soon_image = get_field( 'product_coming_soon_image',get_the_ID());
while($loop->have_posts()) : $loop->the_post();
$termsArray = get_the_terms( get_the_ID(), "product_category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
} ?>
<div class="<?php echo $termsString; ?> isotope-item">
<a href="<?php get_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php the_post_thumbnail_url('') ?>" class="lazy" alt="<?php get_the_title() ?>" loading="lazy" width="240" height="240">
<?php else : ?>
<img src="<?php echo esc_url( $product_coming_soon_image['sizes']['our-products'] ); ?>" alt="<?php echo esc_attr( $product_coming_soon_image['alt'] ); ?>" loading="lazy" width="240" height="240" />
<?php endif; ?>
</a>
<h3><a href="<?php echo get_permalink()(); ?>"><?php get_the_title(); ?></a></h3>
</div>
<?php
endwhile; wp_reset_query();?>
<?php
endif;
endforeach; ?>
</div>
好的,所以你正在做的是:
- 获得一些条件
- 按条件循环
- 按期限获取产品
- 循环显示产品
确实如此,如果一个产品与循环中的多个术语相关联,它们将以这种方式出现多次。
您应该删除第一个 foreach,并通过多个术语查询产品,然后在 wp_query 结果上循环。这样你就不会重复:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => array_column($custom_term, 'slug'),
),
),
);
如果您确实需要根据您的条件进行循环,并在每个循环中查询产品:您需要将所有查询的 post ID 存储在一个数组中,该数组是在循环外启动的。并将此 ID 数组“已查询”添加到查询“post not in: ids”。
我能够通过在第一个 foreach 之前创建一个空数组然后在循环时检查当前 post ID 是否在该数组中来删除重复项。如果当前 post id 不在数组中,将其添加到数组并显示产品。如果当前postid在数组中,则跳过。
更新代码
$product_categories = get_field( 'product_filter_categories' );
$custom_taxonomy = 'product_category';
$custom_terms = $product_categories;
$displayed_products = array();
foreach($custom_terms as $custom_term) :
wp_reset_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => $custom_taxonomy,
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) :
$product_coming_soon_image = get_field( 'product_coming_soon_image' );
while($loop->have_posts()) : $loop->the_post();
if(!in_array( $post->ID, $displayed_products )) :
$termsArray = get_the_terms( $post->ID, "product_category" );
$termsString = "";
foreach ( $termsArray as $term ) {
$termsString .= $term->slug.' ';
} ?>
<div class="<?php echo $termsString; ?> isotope-item">
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail() ) : ?>
<img src="<?php the_post_thumbnail_url('') ?>" class="lazy" alt="<?php the_title() ?>" loading="lazy" width="240" height="240">
<?php else : ?>
<img src="<?php echo esc_url( $product_coming_soon_image['sizes']['our-products'] ); ?>" alt="<?php echo esc_attr( $product_coming_soon_image['alt'] ); ?>" loading="lazy" width="240" height="240" />
<?php endif; ?>
</a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php
array_push( $displayed_products, $post->ID );
endif;
endwhile; ?>
<?php
endif;
endforeach; ?>
'''