在我的网站上,我有一个自定义的帖子类型。每个自定义帖子都有一个人的缩略图,以及一个额外的自定义字段,其中包含另一个图像。我编写了一些代码,以便当用户将鼠标悬停在此人的缩略图上时,可以看到其他缩略图。这是在JS中。
一切都是机械工作正常,(我得到了wp\\u enqueue\\u脚本和wp\\u localize\\u脚本,并从这里获得了一些帮助!)但不是每个人的缩略图都能在悬停时看到正确的对应备选缩略图,而是他们都看到了相同的缩略图。这是读取到数组中的最后一个(它们在循环中都被覆盖)
我的新方法(我不知道它是否正确)是将post ID读入数组的前半部分,然后将相应的缩略图读入下半部分。
当我在PHP中回显它时,这一切似乎都起到了作用,但我不确定如何将其转换为Javascript。
假设postID是23,我试着只写警报(MyScriptParams.23);但这行不通。
我不能完全理解这个范围,如果有100个人我会怎么做?我想我会在JS中编写一个单独的循环来循环所有内容?我很难理解PHP和JS之间的链接。。。
非常感谢您的帮助!我可能在做傻事!
page-custom.php
<?php
$args = array( \'post_type\' => \'drummers\', \'orderby\' => \'menu_order\');
$your_loop = new WP_Query( $args );
//FETCHES THE POSTS
if ( $your_loop->have_posts() ) : while ( $your_loop->have_posts() ) :
$your_loop->the_post();
//GETS THE POST ID FROM WP_QUERY
$gettheid = get_the_ID();
if ( has_post_thumbnail() ) {
the_post_thumbnail(\'\', array( \'class\' => "img-fluid animated drummers_face$post_id"));
}
$meta = get_post_meta( $post->ID, \'drummers_fields\', true );
$thumbnail_url = get_the_post_thumbnail_url($post->ID);
wp_localize_script(\'animated\', \'MyScriptParams\',
$array = array(
$gettheid => $meta[\'image\']
//\'bar\' => $thumbnail_url
));
//THIS ECHOS FINE
echo $array[$gettheid];
?>
<?php the_title(); ?>
<?php the_content(); ?>
</div><!-- END OF COLUMN-->
<?php endwhile; endif; wp_reset_postdata(); ?>
Javascript - 我有一些代码可以在悬停时显示新的缩略图,但我保持它的基本功能,并试图让它首先发出警报alert(MyScriptParams.23);
Update
已将localize\\u脚本移到循环之外,添加到通过数组计数的计数器中,但仍然存在相同的问题。 <?php
$args = array( \'post_type\' => \'drummers\', \'orderby\' => \'menu_order\');
$counter = 0;
$bigid = array();
$your_loop = new WP_Query( $args );
if ( $your_loop->have_posts() ) : while ( $your_loop->have_posts() ) :
$your_loop->the_post();
array_push( $bigid, get_the_ID() );
?>
<div class="col-md-4">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(\'\', array( \'class\' => "img-fluid animated drummers_face$post_id"));
}
$meta = get_post_meta( $post->ID, \'drummers_fields\', true );
$thumbnail_url = get_the_post_thumbnail_url($post->ID);
$array = array(
$bigid[$counter] => $meta[\'image\']
//\'bar\' => $thumbnail_url
);
echo $bigid[$counter];
$counter++;
?>
<?php the_title(); ?>
<?php the_content(); ?>
</div><!-- END OF COLUMN-->
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php wp_localize_script(\'animated\', \'MyScriptParams\', $array); ?>