1

クラス「loopcontent」の 3 つの div ごとに、クラス「threeacross」の新しい div にラップする必要があります。私は WordPress を初めて使用し、実際にはまだ PHP を使用していませんが、学習しながら学んでいます。

ここに私のループがあります:

        <?php if(have_posts()) : while(have_posts()) : the_post(); ?>
            <div class="loopcontent">
                <?php
                    if ( has_post_thumbnail() ) {
                        the_post_thumbnail();
                    } else {
                        // the current post lacks a thumbnail
                    }
                ?>
                <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
                <?php the_excerpt(); ?>
            </div>

        <?php endwhile; else : ?>

            <div class="loopcontent">
                <h1>Page Not Found</h1> 
                <p>Looks like the page you're looking for isn't here anymore. Try browsing the <a href="">categories</a>, <a href="">archives</a>, or using the search box below.</p>
                <?php include(TEMPLATEPATH.'/searhform.php'); ?>
            </div>

        <?php endif; ?>
4

1 に答える 1

7

このようなものがうまくいくはずです。これにより、実行中のカウントが保持され、3 つの投稿ごとに div ラップが配置されます。これが役立つかどうか教えてください。そうでない場合は、何を出力しているか教えてください。微調整できます。

<?php
if(have_posts()) : for($count=0;have_posts();$count++) : the_post();
    $open = !($count%3) ? '<div class="threeacross">' : ''; //Create open wrapper if count is divisible by 3
    $close = !($count%3) && $count ? '</div>' : ''; //Close the previous wrapper if count is divisible by 3 and greater than 0
    echo $close.$open;
?>
<div class="loopcontent">
    <?php
    if(has_post_thumbnail()) the_post_thumbnail();
    else{
    // the current post lacks a thumbnail
    }
    ?>
    <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    <?php the_excerpt(); ?>
</div>
<?php endfor; else : ?>
<div class="loopcontent">
    <h1>Page Not Found</h1> 
    <p>Looks like the page you're looking for isn't here anymore. Try browsing the <a href="">categories</a>, <a href="">archives</a>, or using the search box below.</p>
    <?php include(TEMPLATEPATH.'/searhform.php'); ?>
</div>
<?php endif; ?>
<?php echo $count ? '</div>' : ''; //Close the last wrapper if post count is greater than 0 ?>
于 2012-05-08T23:57:39.810 に答える