2

アイテム (WordPress の投稿) の PHP 配列をループして、投稿の 3x3 グリッドを作成します。
jQuery を使用して、CSS クラスを次のように中央の列の項目に適用します。
$('#grid li:nth-child(3n+2)').addClass('middle');

PHPでこれを達成するにはどうすればよいですか? 一致するカウンターを設定できます2,5,8,11, etc...か?

4

4 に答える 4

1

PHPのループについて知っていますか?PHPコードで何を達成しようとしているのかを正確に知らなくても、次のようなことしか提案できません。

$posts = array(); //The whatever thing that contains the posts you are concerned about
for ($i = 1; $i<=count($posts); $i++) {
    if($i == /*selector condition*/) {
        //do what you do with the targeted posts
    } else {
        //do what you do with all others
    }
}

( http://www.w3schools.com/php/php_looping_for.aspを参照)

ちょっとした補足: 通常は $i=0 でカウントを開始しますが、投稿について話している場合は、おそらく 0 ではなく 1 からカウントを開始すると思います。

于 2012-04-10T17:52:27.050 に答える
1
function nthChild($multiplier, $addition, $max)
{
    $validInedexes = array();

    for ($n = 0; $n < $max; $n++)
    {
        $idx = $multiplier * $n + $addition;
        if ($idx > $max)
        {
            return $validInedexes;
        }
        $validInedexes[] = ($idx - 1);
    }
}

上記の関数は、入力に基づいて有効なインデックスを提供します。次に、インデックスによる任意のループ一致。それまたは好きなものには in_array 関数を使用します。

于 2016-01-29T11:11:37.410 に答える
0

$ my_query = new WP_Query('category_name = special_cat&posts_per_page = 10'); ?>

$query = new WP_Query('...');
$posts = ($count = $query->post_count) ? range(1, $count) : array();
foreach (array_chunk($posts, 3) as $row => $rowIndexes)
{
    foreach ($rowIndexes as $column => $index)
    {
        $query->the_post();
        $middle = $column === 1;
        ...
    }
}
于 2012-04-10T18:00:17.603 に答える
0

:nth-child(3n+2)だろう:

$count = count($arr);
for($i = 0, $idx = 0; $idx < $count - 1; $idx = 3 * $i + 2, $i++) {
    print_r($arr[$idx]);
}

確かに、これはあなたにそれらの特定のアイテムだけを与えます. すべてのアイテムが必要で、それらの要素に対してコード ブロックのみを実行する場合は、わかりません.... まだ.

于 2012-08-30T21:11:23.023 に答える