Php 插入<;tr>;在每三次循环后的表中

Php 插入<;tr>;在每三次循环后的表中,php,woocommerce,html-table,Php,Woocommerce,Html Table,我已经编辑了WooCommerce模板,所以变体标签的输出将在相同的tr中,相应地,变体值将在相同的tr中 到目前为止,它运作良好,但我想改变tr后,第三个td循环 这是我的php: <table class="variations" cellspacing="0"> <tbody> <?php $labels = $values = ''; ?> <?php $loop = 0; foreach ( $attri

我已经编辑了WooCommerce模板,所以变体标签的输出将在相同的tr中,相应地,变体值将在相同的tr中

到目前为止,它运作良好,但我想改变tr后,第三个td循环

这是我的php:

<table class="variations" cellspacing="0">
    <tbody>
        <?php $labels = $values = ''; ?>
        <?php $loop = 0; foreach ( $attributes as $attribute_name => $options ) : $loop++; ?>
        <?php 
            $swatches = theme_has_swatches( $product->get_id(), $attribute_name, $options, $available_variations, $swatches_use_variation_images);
            $active_variations = theme_get_active_variations( $attribute_name, $available_variations );
        ?>

        <?php ob_start(); ?>
        <td class="label" style="padding-right: 15px;min-width: 175px;"><label for="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>"><?php echo wc_attribute_label( $attribute_name ); ?></label></td>
        <?php $labels .= ob_get_contents(); ob_end_clean(); ?>
        <?php ob_start(); ?>
        <td class="value <?php if ( ! empty( $swatches ) ): ?>with-swatches<?php endif; ?>" style="padding-right: 15px;min-width: 175px;">
        <?php if ( ! empty( $swatches ) ): ?>
            <div class="swatches-select swatches-on-single" data-id="<?php echo esc_attr( sanitize_title( $attribute_name ) ); ?>">
            <?php
                if ( is_array( $options ) ) {
                    if ( isset( $_REQUEST[ 'attribute_' . $attribute_name ] ) ) {
                                                $selected_value = $_REQUEST[ 'attribute_' . $attribute_name ];
                    } elseif ( isset( $selected_attributes[ $attribute_name ] ) ) {
                                                $selected_value = $selected_attributes[ $attribute_name ];
                    } else {
                                                $selected_value = '';
                    }

                    // Get terms if this is a taxonomy - ordered
                    if ( taxonomy_exists( $attribute_name ) ) {

                        $terms = wc_get_product_terms( $product->get_id(), $attribute_name, array( 'fields' => 'all' ) );

                        $swatch_size = theme_wc_get_attribute_term( $attribute_name, 'swatch_size' );

                        $_i = 0;
                        $options_fliped = array_flip( $options );
                        foreach ( $terms as $term ) {
                        if ( ! in_array( $term->slug, $options ) ) {
                            continue;
                        }
                        $key = $options_fliped[$term->slug];

                        $style = '';
                        $class = 'theme-swatch swatch-on-single ';

                        $class .= ' swatch-size-' . $swatch_size;

                        if ( $selected_value == $term->slug ) {
                            $class .= ' active-swatch';
                        }

                        echo '<div class="' . esc_attr( $class ) . '" data-value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . ' style="' . esc_attr( $style ) .'">' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</div>';

                        $_i++;
                        }

                    } else {

                        foreach ( $options as $option ) {
                            $class = '';

                            if ( $selected_value == $option ) {
                                $class .= ' active-swatch';
                            }

                            if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $active_variations ) {
                                if ( in_array( $term->slug, $active_variations ) ) {
                                    $class .= ' swatch-enabled';
                                } else {
                                    $class .= ' swatch-disabled';
                                }
                            }

                            echo '<div class="' . esc_attr( $class ) . '" data-value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</div>';
                        }

                    }
                }
            ?>

            </div>

            <?php endif; ?>

            <?php
                wc_dropdown_variation_attribute_options( array(
                    'options'   => $options,
                    'attribute' => $attribute_name,
                    'product'   => $product,
                ) );
                echo end( $attribute_keys ) === $attribute_name ? wp_kses_post( apply_filters( 'woocommerce_reset_variations_link', '<a class="reset_variations" href="#">' . esc_html__( 'Clear', 'woocommerce' ) . '</a>' ) ) : '';
            ?>

            </td>
            <?php $values .= ob_get_contents(); ob_end_clean(); ?>      
            <?php endforeach;?>
            <?php echo '<tr>', $labels,  '</tr><tr>', $values, '</tr>'; ?>
    </tbody>
</table>

必须使用3模的除法

像这样:

  if($loop % 3 == 0) {
     //if true...
  }

提及要修改的模板文件(variable.php)可能会很有用。还有哪些代码行已经更改:(34-54)您是否尝试过调试或打印$\u i?所以你可以看到这在循环过程中是否真的有效?是的,你是对的。基于这篇文章,我使用
ob_start
函数编辑了WooCommerce的variable.php文件,并添加了以下代码
。但是使用“==”不是你应该做的;)为什么不呢?是的,他可以用“==”@maio290@AleksiVirtanen你能编辑你关于我应该如何编辑我的php代码的答案吗?它似乎不起作用。
  if($loop % 3 == 0) {
     //if true...
  }