【发布时间】:2020-04-09 20:26:30
【问题描述】:
我希望将“data-index=”添加到数组中的每个 img 标签。它应该等于它在数组中的数字。例如它会打印"<img class="gallery-item" data-index="1" src="<?php the_field('image'); ?>" alt=""> 等等。有人可以建议如何做到这一点,这是我的代码的样子:
*更新代码以反映答案
HTML
<div id="container" class="isotope" data-element="gallery-item">
<?php
$idx=0;
$args = array(
'post_type' => 'Photos',
);
$_posts = new WP_Query($args);
?>
<?php
if ( $_posts->have_posts() ) : while ( $_posts->have_posts() ) : $_posts->the_post(); ?>
<div class="grid-item" data-filter="<?php
$categories = get_the_category(get_the_id());
foreach ($categories as $category){
echo $category->slug;}?>">
<a onClick='showDialog()' data-target="#lightbox">
<img class="gallery-item" data-index="<?php esc_attr_e( $idx ); ?>" src="<?php the_field('image'); ?>" alt="">
</a>
</div>
<?php
$idx++;
endwhile; endif;
?>
</div>
【问题讨论】:
标签: javascript php arrays wordpress custom-data-attribute