请帮我从特定产品中获取ID。当我点击产品时,随后打开的模式的内容也应该动态更改为该产品。到目前为止,我已经有了模态功能,但我的问题是我无法获得每个产品的确切ID。您是否知道如何在WordPress中获取模态中的产品ID?
<a onclick="modal(this);" id="<?php get_the_id();?>" data-toggle="modal" data-target="#myModal">
Button
</a>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-sm-6">
</div>
<div class="col-sm-6">
</div>
</div>
</div>
</div>
</div>
</div>
&#13;
答案 0 :(得分:1)
希望以下代码能为您提供帮助
<?php
$args['post_type']='product';
$products_query = new WP_Query( $args );
if ( $products_query->have_posts() ) : while ( $products_query->have_posts() ) : $products_query->the_post();
?>
<a onclick="modal(this);" id="<?php echo get_the_ID();?>" data-toggle="modal" data-target="#myModal">Button</a>
<?php
endwhile; endif;
?>
答案 1 :(得分:1)
id是事件对象的属性,因此您只需执行以下操作:
function modal(e) {
console.log(e.id);
}
&#13;
<a onclick="modal(this);" id="<?php get_the_id();?>" data-toggle="modal" data-target="#myModal">
Button
</a>
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<div class="row">
<div class="col-sm-6">
</div>
<div class="col-sm-6">
</div>
</div>
</div>
</div>
</div>
</div>
&#13;