【发布时间】:2020-01-03 12:14:16
【问题描述】:
我正在尝试在 foreach 循环中循环一段 jQuery 代码。循环中的每篇文章都有一个电话号码自定义帖子类型相关 (ACF)。此时循环运行良好。
如您所见,jQuery 代码仅在用户单击以显示号码时将图像替换为另一个图像(例如:“显示电话号码”图像,变为“555-555-1234”)。
问题是当我点击任何图片显示号码时...所有文章同时显示他们的电话号码。我认为是我的 jQuery 代码中的 ID 问题。经过两天的搜索和测试不同的代码,这个问题仍然没有解决。
非常欢迎任何建议/曲目!
谢谢
====
我尝试过的事情:
我尝试将 jQuery 代码放在 foreach 之外(结果相同)
我已尝试将 id 图像更改为类(效果更好)
我尝试过不同的 jQuery 函数(
replaceWith()、show()等)
foreach ($related_posts_articles as $related_post ){
<div class="col-sm-6 col-md-6 col-lg-3 col-xs-12">
<div>
<a href="<?php echo get_permalink($related_post->ID); ?> ">
<?php echo get_the_post_thumbnail($related_post->ID,"square-300",array('class' => 'img-responsive')); ?>
</a>
<div>
<a href="<?php echo get_permalink($related_post->ID); ?>">
<h2>
<?php echo wp_html_excerpt(strip_tags(get_field('acf_titre_mini', $related_post->ID)), 30, '...' ); ?>
</h2>
</a>
<div>
<?php echo wp_html_excerpt( strip_tags(get_field('acf_description_mini',$related_post->ID)), 129, '...' ); ?>
</div>
<!-- START bloc number -->
<?php if( get_field('acf_numero_image', $related_post->ID) ): ?>
<div>
<img class="input_img" src="<?php the_field('acf_btnVoirNum_image', $related_post->ID); ?>" >
<script>
jQuery(document).ready(function($) {
$( ".input_img" ).click(function() {
$( ".input_img" ).attr( "src", "<?php the_field('acf_numero_image', $related_post->ID); ?>" );
});
});
</script>
</div>
<?php endif; ?>
<!-- END bloc number -->
</div>
</div>
</div>
}
【问题讨论】:
标签: php jquery foreach advanced-custom-fields attr