具有多个字词的自定义帖子仅返回一次

时间:2018-09-27 10:15:28

标签: php wordpress advanced-custom-fields

我有一个自定义帖子类型Option Explicit Public Sub GettingURL() Dim i As Long With CreateObject("InternetExplorer.Application") .Visible = True .navigate "https://baidu.com/s?wd=Red" While .Busy Or .readyState < 4: DoEvents: Wend Dim aNodeList As Object: Set aNodeList = .document.querySelectorAll(".pc") For i = 1 To aNodeList.Length - 1 aNodeList.item(i).Click While .Busy Or .readyState < 4: DoEvents: Wend Set aNodeList = .document.querySelectorAll(".pc") Next Stop '<== Delete me later .Quit End With End Sub ,其类别为products。 我还有另一个自定义帖子类型range,它使用ACF stockist将产品与库存商关联。

我试图遍历零售商的职位,返回每个术语的标题,然后通过post对象返回与该零售商相关的每种产品的列表。

post_object

这工作到一定程度,但是产品具有多个术语。例如,猪肉香肠可能既有“香肠”一词,又有“ bbq”一词,在香肠中只返回一次,而在这两个词中都不会返回。

有人可以提供任何建议吗?

1 个答案:

答案 0 :(得分:1)

您的问题在于以下几行:

Return

您正在将循环中的每个$post_term_name = $post_terms_array[0]->slug; if($post_term_name == $category->slug): 与仅与该产品相关联的第一项进行比较。试试看(我只是为了清楚自己而编辑了一些格式)。

$category

我正在创建一个仅包含术语ID的数组,然后检查当前<?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_terms_id_array = wp_list_pluck( $post_terms_array, 'term_id' ); if( in_array( $category->term_id, $post_terms_id_array ) ): ?> <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; ?> ID是否在该数组中。