繁体   English   中英

WP_Query帖子获取循环外的术语列表?

[英]WP_Query posts get term list outside of loop?

也许我正在解决这个问题,但是我遇到了在while循环之外获取信息的问题:

 <?php $title = get_field('car_list_title'); $field = get_field('tax_field_selector'); $query = new WP_Query( array( 'post_type' => 'cars', 'taxonomy' =>'make', 'term' => $field->name, 'posts_per_page' => -1, 'orderby' =>'title', 'order' =>'ASC' ) ); $taxonomy = get_terms( array( 'taxonomy' => 'location', 'hide_empty' => true ) ); if ( $field || $query->have_posts() ) : ?> <div class="c-cars"> <h2 class="c-cars_title ut--underline--lightblue"> <?= $title; ?> </h2> <?php foreach( $taxonomy as $tax ) : $tax_name = $tax->name; ?> <div class="c-cars_row"> <h4 class="c-cars_location-title"> <?= $tax_name; ?> </h4> <div class="c-cars_cars"> <?php while ( $query->have_posts() ) : $query->the_post(); $title = get_the_title(); $link = get_permalink(); $image = get_field('car-picture'); $image_alt = get_field('car_alt'); $image_title = get_field('car_title'); $post_id = get_the_ID(); $terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true)); $location = $terms[0]->name; ?> <?php if( $location === $tax_name ) : ?> <div class="c-cars_car"> <a href="<?= $link; ?>"> <img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>"> </a> <h4 class="text-center"> <a href="<?= $link; ?>"> <?= $title; ?> </a> </h4> </div> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> 

那么这里发生的是我得到的位置列表和这些位置的所有汽车:

位置1:

  • 汽车
  • 汽车
  • 汽车

位置2:

  • 汽车
  • 汽车
  • 汽车

地点3:

地点4:

  • 汽车
  • 汽车
  • 汽车

这里的问题是,例如,即使在该术语中没有“帖子”,位置3也会出现。

while循环只是特定型号的汽车,按其所在位置分类。

我不确定如何过滤掉空位。

我做:

<?php if( $location === $tax_name ) : ?>

在循环内部,它们将它们从位置过滤掉,但仍留下位置标题,因为它在while循环之外。 如果我能够在代码中更早地执行此操作,它可能会工作,但我无法获取while循环之外的活动术语列表。

我现在有点迷失了。 任何想法或建议? 谢谢!

你可以检查使用条件,如果有一个术语所以显示标题要么它将是空白尝试它低于条件,并在评论中提到,如果它的工作与否。

has_terms https://developer.wordpress.org/reference/functions/has_term/

功能检查帖子是否有条款。

if( has_term( $location, $tax_name ) ) {
// do something
}

好的,我已经更新了你的答案,请在下面的代码中尝试。 如果条款有帖子计数,我只是得到条款和适用条件的帖子数,所以显示条款标题名称或如果没有条款的帖子计数,那么标题将显示为空白。

 <?php $title = get_field('car_list_title'); $field = get_field('tax_field_selector'); $query = new WP_Query( array( 'post_type' => 'cars', 'taxonomy' =>'make', 'term' => $field->name, 'posts_per_page' => -1, 'orderby' =>'title', 'order' =>'ASC' ) ); $taxonomy = get_terms( array( 'taxonomy' => 'location', 'hide_empty' => true ) ); if ( $field || $query->have_posts() ) : ?> <div class="c-cars"> <h2 class="c-cars_title ut--underline--lightblue"> <?= $title; ?> </h2> <?php foreach( $taxonomy as $tax ) : $tax_name = $tax->name; $tax_post_count = $tax->count; ?> <div class="c-cars_row"> if ( $tax_post_count > 0 ) : ?> <h4 class="c-cars_location-title"> <?= $tax_name; ?> </h4> <?php else : ?> <h4 class="c-cars_location-title"> <?= $tax_name = ''; ?> </h4> <?php endif; ?> <div class="c-cars_cars"> <?php while ( $query->have_posts() ) : $query->the_post(); $title = get_the_title(); $link = get_permalink(); $image = get_field('car-picture'); $image_alt = get_field('car_alt'); $image_title = get_field('car_title'); $post_id = get_the_ID(); $terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true)); $location = $terms[0]->name; ?> <?php if( $location === $tax_name ) : ?> <div class="c-cars_car"> <a href="<?= $link; ?>"> <img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>"> </a> <h4 class="text-center"> <a href="<?= $link; ?>"> <?= $title; ?> </a> </h4> </div> <?php endif; ?> <?php endwhile; wp_reset_postdata(); ?> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM