Woocommerce -- Wordpress 单选按钮需要处于活动状态

时间:2021-04-26 08:11:31

标签: wordpress woocommerce radio-button wp-list-categories

我试图在按下该选项时激活单选按钮。这可能很容易,但我已经有一段时间了。 代码如下:

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

如果有人对如何在该变量中激活单选按钮有任何想法,请提供帮助。我已经研究了好几个小时了。

1 个答案:

答案 0 :(得分:0)

好吧,它只是checked(对或错)而不是checked=checked!您的 html 语法不正确!

您可以阅读有关单选输入语法的更多信息:

<块引用>

html radio syntax

如果您需要 php 条件检查,那么您可以执行以下操作:

<input type="radio" <?php echo ($product_start == $product->ID ) ? "checked" : "" ?> >