我想覆盖 Woocommerce 输出属性的方式。默认情况下,属性显示在两列中 - 第 1 列是标签,第 2 列是逗号分隔的属性列表。我想覆盖它并使每个属性显示在自己的单元格中。原始代码如下:
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
?>
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<th><?php echo $woocommerce->get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?></th>
<td><?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
我一直认为我需要在
foreach
中设置另一个 td
语句,但我不确定如何设置它。
基本上,使用该代码,您可以得到以下输出:
<tr>
<th>atribute label</th>
<td>atribute name</td>
<tr>
所以,你只需要输出类似:“属性标签 - 属性名称”,因为你只需要删除 th 标签并将标签的代码放在 td 标签中。
像这样:
<tr class="<?php if ( ( $alt = $alt * -1 ) == 1 ) echo 'alt'; ?>">
<td>
<?php echo $woocommerce->get_helper( 'attribute' )->attribute_label( $attribute['name'] ); ?>
<?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
} else {
// Convert pipes to commas and display values
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?></td>
<?php endforeach; ?>
</tr>
我想通了...我需要调整
foreach
语句以及单元格在行中的显示方式(以及其中显示的内容),这是我正在运行的最终代码,但是,我希望得到改进的建议它:
<?php foreach ( $attributes as $attribute ) :
if ( empty( $attribute['is_visible'] ) || ( $attribute['is_taxonomy'] && ! taxonomy_exists( $attribute['name'] ) ) )
continue;
?>
<tr>
<th scope="row"><?php echo $woocommerce->attribute_label( $attribute['name'] ); ?></th>
<?php
if ( $attribute['is_taxonomy'] ) {
$values = woocommerce_get_product_terms( $product->id, $attribute['name'], 'names' );
foreach ( $values as $value ) :
echo '<td>';
echo $value;
echo '</td>';
endforeach;
} else {
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
foreach ( $values as $value ) :
echo '<td>';
echo $value;
echo '</td>';
endforeach;
}
?>
</tr><?php endforeach; ?>