我在WordPress中设置了自定义帖子类型并有问题。如何让WordPress将查询中的最后一项分配给另一个类?
这是我的代码:(这很完美......)
<?php
$args = array('post_type' => 'testimonial_quote', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="quote-main">
<p class="quote"><?php echo get_field('testimonial_item'); ?></p>
<p class="name"><?php echo get_field('testimonial_name_item'); ?></p>
<p class="company"><?php the_title(); ?></p>
</div>
<?php
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
对于我列表中的最后一项 - 我需要它:
<div class="quote-main-final">
<p class="quote"><?php echo get_field('testimonial_item'); ?></p>
<p class="name"><?php echo get_field('testimonial_name_item'); ?></p>
<p class="company"><?php the_title(); ?></p>
</div>
(基本上将“quote-main”的类名更改为“quote-main-final”以获取最终项目)
任何帮助都将不胜感激。
答案 0 :(得分:2)
您可以计算结果
<?php
$args = array('post_type' => 'testimonial_quote', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// Count your recordset
$counter = $the_query->post_count;
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="quote-main<?php echo(($i == $counter - 1)?'-final':'');?>">
<p class="quote"><?php echo get_field('testimonial_item'); ?></p>
<p class="name"><?php echo get_field('testimonial_name_item'); ?></p>
<p class="company"><?php the_title(); ?></p>
</div>
<?php
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>