简体   繁体   English

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

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

Maybe I'm going about this incorrectly but I'm having an issue with getting info outside of the while 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; ?> 

So what happens here is I get a list of the locations and all the cars in those locations: 那么这里发生的是我得到的位置列表和这些位置的所有汽车:

Location 1: 位置1:

  • Car 汽车
  • Car 汽车
  • Car 汽车

Location 2: 位置2:

  • Car 汽车
  • Car 汽车
  • Car 汽车

Location 3: 地点3:

Location 4: 地点4:

  • Car 汽车
  • Car 汽车
  • Car 汽车

The problem here is, as an example, Location 3 shows up even though there's no "posts" in that term. 这里的问题是,例如,即使在该术语中没有“帖子”,位置3也会出现。

The while loop is only cars of a specific model, sorted into what location they are at. while循环只是特定型号的汽车,按其所在位置分类。

I'm not really sure how to filter out the empty locations. 我不确定如何过滤掉空位。

I do: 我做:

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

Inside of the loop and that filters them out of the locations but still leaves the location title because it's outside of the while loop. 在循环内部,它们将它们从位置过滤掉,但仍留下位置标题,因为它在while循环之外。 If I were able to do this earlier up in the code it may work but I can't get the list of active terms outside of the while loop. 如果我能够在代码中更早地执行此操作,它可能会工作,但我无法获取while循环之外的活动术语列表。

I'm kind of lost right now. 我现在有点迷失了。 Any ideas or suggestions? 任何想法或建议? Thanks! 谢谢!

you can check using condition if has a term so show title either it will be blank try it below condition and mentioned in a comment if it's work or not. 你可以检查使用条件,如果有一个术语所以显示标题要么它将是空白尝试它低于条件,并在评论中提到,如果它的工作与否。

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

function check if the post has terms. 功能检查帖子是否有条款。

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

Okay, I have updated your answer please try it below code. 好的,我已经更新了你的答案,请在下面的代码中尝试。 I have just get post count of terms and applied condition if terms has post count so show terms title name or if there is no post count of terms so the title will show blank. 如果条款有帖子计数,我只是得到条款和适用条件的帖子数,所以显示条款标题名称或如果没有条款的帖子计数,那么标题将显示为空白。

 <?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