自定义 Wordpress 循环 HTML/CSS

Wordpress Loop For Custom HTML/CSS

我是 PHP 的新手,我正在尝试做一些可能很容易的事情,但我似乎无法让它发挥作用。我正在尝试设置一个 WordPress 循环,以便它为前 3 个加载一种样式,然后为接下来的 6 个加载另一种样式,依此类推。

到目前为止,我尝试过使用计数器,但没有成功。这个想法是主页上的前三个帖子将在大图像上方带有标题和元数据,在图像下方带有摘录。接下来的 6 个将在左侧带有方形图像,在右侧带有标题和摘录。我能想到的唯一方法是使用两个单独的 "content" 页面。

如有任何帮助,我们将不胜感激!

            <?php while ( have_posts() ) : the_post(); ?>
                <?php $count = 1; ?>
                <?php if ($count <=3) { ?>
                    <?php get_template_part( 'content', '' );
                    $count++; ?>
                <?php } if ($count > 3) { 
                    get_template_part( 'content', 'small' );
                } ?>

            <?php endwhile; ?>

如果您想继续使用当前的方法,这应该可以解决您的问题。 只需在 循环之前声明并初始化 $count ,否则您总是将其值重置为 1.

<?php $count = 0; ?>
<?php while ( have_posts() ) : the_post(); ?>
    <?php $count++; ?>
    <?php if ($count <=3) { ?>
        <?php get_template_part( 'content', '' ); ?>
    <?php } if ($count > 3) { 
        get_template_part( 'content', 'small' );
    } ?>
<?php endwhile; ?>