我需要一些关于此代码的帮助

时间:2017-07-30 14:49:32

标签: javascript php

有人可以向我解释这段代码究竟是做什么的吗?我对“额外职位课”部分感到特别困惑。我唯一想要实现的是每四个班级都有一个额外的课程。

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

1 个答案:

答案 0 :(得分: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块类似似乎是合乎逻辑的:

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

请注意,模数运算符%会返回除法运算的其余部分,因此如果它正在运行,但它没有得到"对"一组列尝试将0更改为123