【发布时间】:2026-01-19 22:00:01
【问题描述】:
我有一个自定义帖子类型 products,它的分类是 range。
我有另一个自定义帖子类型 stockist,它使用 ACF post_object 将产品与库存商相关联。
我正在尝试遍历库存商帖子,返回每个术语的标题,然后通过帖子对象返回与库存商关联的每个产品的列表。
<?php
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown STORE <?php the_title();?>">
<h2 class="stockist-title"><?php the_title();?></h2>
<?php
$args = array( 'taxonomy' => 'range');
$categories = get_categories($args);
if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
<div class="col-1-5 mobile-col-1-2">
<h4><?php echo ($category->name) ;?></h4>
<?php $post_objects = get_field('stocked_range'); if( $post_objects ): ?>
<ul class="stockist-block-products clearfix">
<?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug;
if($post_term_name == $category->slug):?>
<li>
<a href="<?php echo get_permalink($post_object->ID); ?>" target="_blank"><?php echo get_the_title($post_object->ID); ?></a>
</li>
<?php endif; endforeach; ?>
</ul>
<?php endif;?>
</div>
<?php endforeach; endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
这在某种程度上有效,但产品有多个术语。例如,pork sausage 可能有术语“sausages”和“bbq”,它只在 sausages 下返回一次,而不是在两个术语下都返回。
任何人都可以提供任何建议吗?
【问题讨论】:
标签: php wordpress advanced-custom-fields