Php WP Loop Geo+;jQuery.hide()工作不正常
我有一个WP循环,每个帖子都收集了4张基于国家的图片(使用ACF) 我只想输出一个国家的图像,但它是显示每个职位的所有4个图像Php WP Loop Geo+;jQuery.hide()工作不正常,php,jquery,wordpress,loops,advanced-custom-fields,Php,Jquery,Wordpress,Loops,Advanced Custom Fields,我有一个WP循环,每个帖子都收集了4张基于国家的图片(使用ACF) 我只想输出一个国家的图像,但它是显示每个职位的所有4个图像 <?php $args = array( 'post_type' => 'quick_links', 'posts_per_page' => 3 ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); $image_au
<?php
$args = array( 'post_type' => 'quick_links', 'posts_per_page' => 3 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$image_au = get_field("au_image");
$image_nz = get_field("nz_image");
$image_us = get_field("us_image");
$image_gl = get_field("global_image"); //default image
?>
<script type="text/javascript">
var image_au = <?php echo json_encode($image_au['url']); ?>;
var image_nz = <?php echo json_encode($image_nz['url']); ?>;
var image_us = <?php echo json_encode($image_us['url']); ?>;
var image_gl = <?php echo json_encode($image_gl['url']); ?>;
jQuery.get("http://ipinfo.io", function (response) {
if (response.country === "AU"){
jQuery("#resultQLAU").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLGlobal").hide();
} else if(response.country === "NZ"){
jQuery("#resultQLNZ").show();
jQuery("#resultQLAU").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLGlobal").hide();
} else if(response.country === "US"){
jQuery("#resultQLUS").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLAU").hide();
jQuery("#resultQLGlobal").hide();
} else {
jQuery("#resultQLGlobal").show();
jQuery("#resultQLNZ").hide();
jQuery("#resultQLUS").hide();
jQuery("#resultQLAU").hide();
}
if(image_au === "" && image_nz === "" && image_us === "" && image_gl !== ""){
jQuery("#resultQLGlobal").show();
}
}, "jsonp");
</script>
<?php
echo '<div class="col-lg-4 col-sm-6" style="padding:2px">';
echo '<a href="' . get_field('page_url') . '" class="portfolio-box">';
?>
<div id="resultQLAU">
<img class="img-responsive" src="<?php echo $image_au['url']; ?>" alt="<?php echo $image_au['alt']; ?>" />
</div>
<div id="resultQLNZ">
<img class="img-responsive" src="<?php echo $image_nz['url']; ?>" alt="<?php echo $image_nz['alt']; ?>" />
</div>
<div id="resultQLUS">
<img class="img-responsive" src="<?php echo $image_us['url']; ?>" alt="<?php echo $image_us['alt']; ?>" />
</div>
<div id="resultQLGlobal">
<img class="img-responsive" src="<?php echo $image_gl['url']; ?>" alt="<?php echo $image_gl['alt']; ?>" />
</div>
<?php
echo '<div class="portfolio-box-caption">';
echo '<div class="portfolio-box-caption-content">';
echo '<div class="project-category text-faded">' . get_the_title() . '</div>';
echo '<div class="project-name">' . get_the_content() . '</div>';
echo '</div>';
echo '</div>';
echo '</a>';
echo '<h6 class="news-title text-center"><a href="' . get_field('page_url') . '">' . get_the_title() . '<span style=""> <i class="fa fa-angle-double-right"></i></span></a></h6>';
echo '</div>';
endwhile;
?>
var image_au=;
var image_nz=;
var image_us=;
var image_gl=;
jQuery.get(“http://ipinfo.io“,功能(响应){
如果(response.country==“AU”){
jQuery(“#resultQLAU”).show();
jQuery(“#resultQLNZ”).hide();
jQuery(“#resultglus”).hide();
jQuery(“#resultglobal”).hide();
}否则如果(response.country==“NZ”){
jQuery(“#resultQLNZ”).show();
jQuery(“#resultQLAU”).hide();
jQuery(“#resultglus”).hide();
jQuery(“#resultglobal”).hide();
}否则如果(response.country==“美国”){
jQuery(“#resultglus”).show();
jQuery(“#resultQLNZ”).hide();
jQuery(“#resultQLAU”).hide();
jQuery(“#resultglobal”).hide();
}否则{
jQuery(“#resultglobal”).show();
jQuery(“#resultQLNZ”).hide();
jQuery(“#resultglus”).hide();
jQuery(“#resultQLAU”).hide();
}
如果(图像au===“”&图像nz==“”&图像us==“”&图像gl!==“”){
jQuery(“#resultglobal”).show();
}
}“jsonp”);
我最初有代码,例如
,而刚刚有jQuery(#resultQLAU”).show()仅输出第一个的脚本中的代码>
第一篇文章的地理图像(因此GEO对这1篇文章的工作是正确的)
不确定是什么问题
非常感谢你的帮助。谢谢您在循环中使用了ID,因此所有块都将具有相同的ID,这并不好,因为ID需要是唯一的。您可以通过根据迭代添加后缀/前缀来改变这一点,并改用类
1) 在循环中添加一个新变量,如下所示:
$i = 0
while ( $loop->have_posts() ) : $loop->the_post();
$i++;
2) 对于每个id,附加$i的内容,例如:
jQuery(".resultQLAU_<?php echo $i; ?>").show();
jQuery(“.resultklau”).show();
在你有id的任何地方都要这样做。谢谢你的回答,不幸的是我无法让这个工作。在我将id制作成一个类并工作之前,谢谢你的帮助!太好了,我正在更新我的anwser;)类总是比ID更好