2017-07-30 340 views
-5

有人可以向我解釋這段代碼究竟幹了什麼嗎?我特別對'額外職位類'部分感到困惑。我試圖達到的唯一目標是每4個班級都有一個額外的班級。我需要一些關於此代碼的幫助

global $product, $woocommerce_loop; 

$PHT_shopID = get_option('woocommerce_shop_page_id'); 
$PHT_shopID = get_permalink($PHT_shopID); 

// Store loop count we're currently on 
if (empty($woocommerce_loop['loop'])) { 
    $woocommerce_loop['loop'] = 0; 
} 

// Store column count for displaying the grid 
if (empty($woocommerce_loop['columns'])) { 
    $woocommerce_loop['columns'] = apply_filters('loop_shop_columns', 4); 
} 

// Ensure visibility 
if (!$product || !$product->is_visible()) { 
    return; 
} 

// Increase loop count 
$woocommerce_loop['loop']++; 

// Extra post classes 
$PHTclasses = array(); 
if (0 === ($woocommerce_loop['loop']) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns']) 
    $PHTclasses[] = 'first'; 
if (0 === ($woocommerce_loop['loop']) % $woocommerce_loop['columns']) 
    $PHTclasses[] = 'last'; 

    $PHTclasses[] = 'primary-product-block'; 
?> 
+2

這是什麼部分讓你感到困惑? – Carcigenicate

+0

當這些課程獲得一個額外的課@Carcigenicate – Bokdem

+4

我投票結束這個問題作爲題外話,因爲[so]不是用於解釋什麼代碼 –

回答

1

我添加了一些意見,以你的IFS來解釋他們

// Extra post classes 
$PHTclasses = array(); 

// if iterator is evenly divisible by # columns, or if there is only one column, add "first" 
if (0 === ($woocommerce_loop['loop']) % $woocommerce_loop['columns'] || 1 === $woocommerce_loop['columns']) 
    $PHTclasses[] = 'first'; 

// if iterator is evenly divisible by # columns, add "last" 
if (0 === ($woocommerce_loop['loop']) % $woocommerce_loop['columns']) 
    $PHTclasses[] = 'last'; 

因此,似乎合乎邏輯的,如果塊類似再添:

// if iterator is evenly divisible by 4 
if (0 === ($woocommerce_loop['loop']) % 4) 
    $PHTclasses[] = 'mynewclass'; 

注意,模運算%返回除法運算的其餘部分,所以如果它正在工作但它沒有得到「正確」的列集嘗試將0更改爲123

+0

非常感謝,這個解釋正是我所期待的。剩下的%對我來說是新的東西,我想這只是我必須習慣的。 – Bokdem