简体   繁体   English

如何在Wordpress的Bootstrap Modal中获取帖子ID

[英]How to get Post ID in bootstrap Modal in Wordpress

Please help me to get the ID from specific products. 请帮助我从特定产品获取ID。 When I click on the product, the content of a modal that subsequently opens should also change dynamically to that product. 当我单击产品时,随后打开的模式的内容也应动态更改为该产品。 So far I already have the modal functioning, but my problem is I cannot get the exact ID of every product. 到目前为止,我已经具有模态功能,但是我的问题是我无法获得每个产品的确切ID。 Do you have any idea on how can I can get the product ID inside the modal in WordPress? 您对如何在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> 

Hope the below code will help you 希望下面的代码对您有所帮助

<?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;

    ?>

The id is a property of the event object so you simply do the following: id是事件对象的属性,因此您只需执行以下操作:

 function modal(e) { console.log(e.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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM