html - Woocommerce如何在需要激活时检查单选按钮
问题描述
我试图在按下选项时激活单选按钮。这可能很容易,但我已经有一段时间了。
这是代码:
<?php
$args = array(
'post_type' => 'product',
'numberposts' => -1,
'order' => 'ASC',
'tax_query' => [
[
'taxonomy' => 'product_cat',
'terms' => 89,
'include_children' => false // Remove if you need posts from term 7 child terms
],
],
);
$products = get_posts( $args );
$product_start = 1;
foreach($products as $product):
$product_s = wc_get_product( $product->ID );
if ($product_s->product_type == 'variable') {
if($c_p_id == '') {
$c_p_id = $product->ID;
}
?>
<li class="col-md-6 col-sm-6 col-xs-12 step1 <?php if($c_p_id == $product->ID) { echo "active"; } ?> ">
<a href="<?php echo $page_path; ?>?c_p_id=<?php echo $product->ID; ?>">
<div class="hldr">
<h2><?php echo get_the_title( $product->ID ); ?></h2>
<div class="img-hldr">
<?php echo $image = wp_get_attachment_image( get_post_thumbnail_id( $product->ID ), array('391', '266') , true );?>
<!-- <img src="<?php //bloginfo('template_url'); ?>/images/stand_alone2_375_250auto_bor8_ffffff_s_c1.jpg" alt="" />-->
<div class="squaredFour">
<input type="radio" value="<?php echo $product->ID ; ?>" name="product" <?php if($product_start == $product->ID ) { echo "checked='checked'"; } ?> class="checkTrack">
<!-- <input type="radio" value="<?php echo $product->ID ; ?>" name="product" id="product" <?php if($product_start == $product->ID ) { echo "checked='checked'"; } ?> class="checkTrack"> -->
</div>
<div class="txtDiv">
<h3>Step1 :</h3>
<h3>Traditional</h3>
</div>
</div>
</div>
</a>
</li>
<?php
}
if($c_p_id == $product->ID) {
$tradi_product = new WC_Product_Variable($product->ID);
$tradi_default_attributes = $tradi_product->get_default_attributes();
$defaultProductId = $product->ID;
}
$product_start = 2;
endforeach;
?>`
如果有人对如何在该变量中激活单选按钮有任何想法,请提供帮助。
我已经做了好几个小时了。
解决方案
好吧,这只是checked
(真或假)NOT checked=checked
!您的 html 语法不正确!
您可以阅读有关无线电输入语法的更多信息:
如果您需要php
条件检查,那么您可以执行以下操作:
<input type="radio" <?php echo ($product_start == $product->ID ) ? "checked" : "" ?> >
推荐阅读
- r - 在 R 中使用矢量多边形提取光栅像素值
- django - django 测试期间数据库创建失败
- python - PermissionError: [Errno 13] Permission denied - Python/Django Locallibrary
- java - 弹出通知时如何播放声音?
- javascript - 使用 ref 访问元素内的元素
- r - 如何使用ggplot2在直方图中添加汇总统计?
- angular - 如何在 Angular 8 应用程序中设置 expires_?
- c++ - 如何找出我的 C++ 编译器用于 std::hash 的特定算法?
- php - PHP 函数 exec() 停止加载站点
- android - 在服务类中使用 picasso 加载图像