我有一个值数组,我想要每个项目的索引/键值,以便我可以使用该值将其用作类,以进行样式设置。我该如何实现?
这是我到目前为止所得到的:
android:textAlignment="center"
我希望输出是这样的:
$args = array(
'post_type' => array('case'),
'post_status' => array('publish'),
'posts_per_page' => 5
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts() ){
$query->the_post();
?>
<div class="grid-item item--HERE I WANT THE KEY/INDEX VALUE">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
<?php }
}
答案 0 :(得分:1)
答案 1 :(得分:0)
根据您的实际需求,可以采用以下几种方法之一。要使用增量值:
<div class="grid-item item--<?php echo esc_attr( $query->current_post + 1 ); ">
或使用帖子ID:
<div class="grid-item item--<?php echo esc_attr( $post->ID ); ">
或使用后:
<div class="grid-item item--<?php echo esc_attr( $post->post_name ); ">
请注意,我使用esc_attr()
来清理输出,这被认为是安全性的最佳做法,例如,如果要发布到WordPress VIP,则需要这样做。