1

ブログを3つの列に分割する必要があります。そのために、私は書いた:

<?php $i = 0; ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 0) : $wp_query->next_post(); else : the_post(); ?>
    <?php get_template_part( 'content', 'category' ); ?>
        <?php endif; endwhile; endif;  ?>
    </div>

    <?php $i = 0; rewind_posts(); ?>

<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 1) : $wp_query->next_post(); else : the_post(); ?>
    <?php get_template_part( 'content', 'category' ); ?>
        <?php endif; endwhile; endif;  ?>
    </div>

<?php $i = 0; rewind_posts(); ?>

<div class="onethird last">
<?php if (have_posts()) : while(have_posts()) : $i++; if(($i % 3) == 2) : $wp_query->next_post(); else : the_post(); ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>

それは非常に悪いコードだと確信していますが、それは私が思いついた最高のものでした。次に、重複した投稿の問題に気づきました。繰り返しますが、グーグルをたくさんした後、間違っています:

<?php $i = 0; $dupe = array(); ?>
<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 0) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif;  ?>
</div>

<?php $i = 0; rewind_posts(); ?>

<div class="onethird">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 1) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif;  ?>
</div>

<?php $i = 0; rewind_posts(); ?>

<div class="onethird last">
<?php if (have_posts()) : while(have_posts()) : $i++; if( (($i % 3) == 2) && (!in_array($post->ID, $dupe)) ) : $wp_query->next_post(); else : the_post(); ?>
<?php $dupe[] = $post->ID; echo $post->ID ?>
    <?php get_template_part( 'content', 'category' ); ?>
<?php endif; endwhile; endif; ?>
</div>

これで、カウンターと、が無視in_arrayされ、3列すべてにすべての投稿が表示されます。

誰かが3列に投稿を表示するためのより良い解決策を持っているなら、それも歓迎されます!

4

2 に答える 2

1

3つのループは、達成しようとしていることに対して少し複雑に見えます。

投稿の総数を取得します---wp_count_posts()そしてそれを3で割って(切り上げ)、列ごとの投稿の数を取得します。

次に、ループを1回行うだけ</div><div class='onethird'>で、カウンターの残りの部分を$i列ごとの投稿数で割った値が。になるたびに追加します0

何かのようなもの:

<div class="onethird">
<?php
$a_third = ceil(wp_count_posts() / 3);
$i = 0;
if (have_posts()) :
  while(have_posts()) :

    $i++;

    the_post();

    if( ($i % $a_third) === 0 ) :
      echo "</div><div class='onethird'>";
    endif;

  endwhile;
endif;
?>
</div>
于 2013-01-31T15:28:31.100 に答える
0

私はあなたがこれであなたが持っているもので働くことができると思います:

<?php $i = 0; $dupe = array(); $third = ceil(wp_count_posts() / 3);?>
<?php if (have_posts()) : ?>
    <div class="onethird">
        <?php while(have_posts()) : the_post(); ?>
            <?php if (($i < $third) && (!in_array($post->ID, $dupe))) : ?>
                <?php $dupe[] = $post->ID; ?>
                <?php echo $post->ID; ?>
                <?php get_template_part( 'content', 'category' ); ?>
                <?php $i++; ?>
            <?php endif; ?>
        <?php endwhile; ?>
    </div>

    <?php $i = 0; rewind_posts(); ?>

    <div class="onethird">
        <!-- repeat same code as above -->
    </div>

    <?php $i = 0; rewind_posts(); ?>

    <div class="onethird">
        <!-- repeat same code as above -->
    </div>

<?php endif; ?>
于 2013-01-31T16:12:35.120 に答える