如何为循环中返回的每个项目分配一个唯一的类?

我试图在PHP中获得一些帮助,这样我就可以为循环中返回的每个项添加一个类或ID。 我使用一段代码(见下文)来返回分类法(位置)。 我将所有32个位置完美地返回并显示在我的网站上。 但是,我无法通过css单独设置每个位置的样式,因为没有与每个位置关联的唯一类。

返回的每个块在前端看起来都是这样的

对于所有位置重复返回。 正如你所看到的那样,没有办法真正地为每个单独设置样式,这就是我的要求。

我返回位置的代码如下。 我尝试使用谷歌搜索和一些不同的方法,但感觉我离开了我的联盟,并认为我会请专家在这里寻求帮助!

谢谢

 name); if ($cpt_test == 'true') { $cpt_nice_name = $post_type_item->labels->name; $cpt_has_archive = $post_type_item->has_archive; $post_types_to_loop_through[$post_type_item->name] = array('nice_name' => $cpt_nice_name, 'has_archive' => $cpt_has_archive); } } $section_counter = 0; foreach ($post_types_to_loop_through as $post_type_item => $post_type_item_nice_name) { $taxonomies = array('location'); $block_counter = 0; $args = array('orderby' => 'name', 'order' => 'ASC'); ?> 
taxonomy); if ( ($tax_test == 'true') && ($block_counter name; $term_slug = $all_term->slug; $term_id = $all_term->term_id; $term_link = get_term_link( $all_term, $all_term->taxonomy ); $counter_value = $all_term->count; ?>
<a href="https://stackoverflow.com/questions/12377711/how-to-assign-a-unique-class-to-each-item-returned-in-a-loop/"> 0 ) { // If a featured image is available, use it in priority over the "image" field. if ( function_exists( 'has_post_thumbnail' ) && current_theme_supports( 'post-thumbnails' ) ) { if ( has_post_thumbnail( $post_id ) ) { $_id = 0; $_id = get_post_thumbnail_id( $post_id ); if ( intval( $_id ) ) { $_image = array(); $_image = wp_get_attachment_image_src( $_id, 'full' ); // $_image should have 3 indexes: url, width and height. if ( count( $_image ) ) { $_image_url = $_image[0]; woo_image('src=' . $_image_url . '&key=image&width=139&height=81&link=img'); } // End IF Statement } // End IF Statement } else { woo_image('id='.$post_id.'&key=image&width=139&height=81&link=img'); } // End IF Statement } else { woo_image('id='.$post_id.'&key=image&width=139&height=81&link=img'); } // End IF Statement } else { // Fallback woo_taxonomy_image($post_images,$term_link); } // End IF Statement $php_formatting = "m\/d\/Y"; $post_item_date = strtotime($post_date_raw); ?>

<a href="https://stackoverflow.com/questions/12377711/how-to-assign-a-unique-class-to-each-item-returned-in-a-loop/">
( Farmers)

0 ) { ?>

更新感谢我已经实现此代码的一些帮助,但仍无法使其工作!

我将此添加到第8行的全局变量$i = 0; // initialise incrementing variable $i = 0; // initialise incrementing variable

然后我在div之前添加了这个,因为你可以看到我试图回应的额外类

 // I added this line as suggested just before my div  // I then added a new class but I was not sure how to write this <div class="block" class=""> 

就在结束div标签之前(大约100行之后),我添加了结束代码

  $i++; // increment it } 

正如@sushanth reddy所说,它应该真的如此简单

 $i = 0; // initialise incrementing variable foreach ( $all_terms as $all_term) { // rest of your code; use $i when you want your incrementing ID / class $i++; // increment it }