我可以通过在表行中选择的自定义属性名称(如“颜色”、“尺寸”、“重量”等)来获取属性,但我只想显示 3 行。 我的工作代码如下,但它显示了所有代码,我只想显示 3 行
add_action( 'cw_shop_page_attribute', 'cw_shop_page_attribute', 25 );
function cw_shop_page_attribute() {
global $product;
$display_size = $product->get_attribute('display-size');
$processor = $product->get_attribute('processor-type');
$rearcamera = $product->get_attribute('primary-camera');
$frontcamera = $product->get_attribute('secondary-camera');
$storage = $product->get_attribute('internal-storage-gb');
$m_ram = $product->get_attribute('ram-gb');
$frontcamera = $product->get_attribute('secondary-camera');
if ( $display_size ) {
echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-desktop fa-2x"></i></td><td class="_atrbttl">Display</td>';
echo'<td class="_atrbvlu">'; printf ($display_size);
echo'</td></tr>';
}
if ( $processor ) {
echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-microchip fa-2x"></i></td><td class="_atrbttl">Processor</td>';
echo'<td class="_atrbvlu">'; printf ($processor);
echo'</td></tr>';
}
if ( $rearcamera ) {
echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-camera fa-2x"></i></td><td class="_atrbttl">Rear Camera</td>';
echo'<td class="_atrbvlu">'; printf ($rearcamera);
echo'</td></tr>';
}
if ( $frontcamera ) {
echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-camera fa-2x"></i></td><td class="_atrbttl">Front Camera</td>';
echo'<td class="_atrbvlu">'; printf ($frontcamera);
echo'</td></tr>';
}
如何仅显示其中的 3 行并在为空时隐藏
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
get_attribute () 返回以逗号分隔的值字符串,因此您可以使用 php 的explode 函数以数组形式循环遍历这些值,然后在返回 3 个结果后退出。
例如:
if ( $display_size ) { echo'<tr class="_plspcdt"><td class="_plspcicon"><i class="fa fa-desktop fa-2x"></i></td><td class="_atrbttl">Display</td>'; $display_size_array = explode( ',', $display_size ); $count = 0; foreach ( $display_size_array as $attribute ) { if ( $count >= 3 ) { break; } echo'<td class="_atrbvlu">' . $attribute . '</td'; $count++; } echo'></tr>'; }