So beschränken Sie Produktattribute in der Woocommerce-Kundenvorlage
P粉191323236
P粉191323236 2023-09-07 19:42:32
0
1
536

Ich kann Eigenschaften erhalten, indem ich benutzerdefinierte Eigenschaftsnamen in Tabellenzeilen auswähle (wie „Farbe“, „Größe“, „Gewicht“ usw.), aber ich möchte nur 3 Zeilen anzeigen. Mein Arbeitscode ist unten, aber er zeigt den gesamten Code und ich möchte nur 3 Zeilen anzeigen

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>';
   }

So zeigen Sie nur 3 Zeilen davon an und verstecken sie, wenn sie leer sind

P粉191323236
P粉191323236

Antworte allen(1)
P粉691958181

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>';
}
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!