Products
GG网络技术分享 2025-03-18 16:12 0
Please help me to get the ID from specific products. 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. Do you have any idea on how can I can get the product ID inside the modal in WordPress?
<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>
</div>
The id is a property of the event object so you simply do the following:
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>
</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;
?>
Demand feedback