WordPress列表页面与缩略图交替显示


WordPress list pages alternating with thumbnails

在我的首页上,我想显示页面,但缩略图大小不同。我有两个缩略图大小应该使用
第1页:尺寸A
第2页:尺寸B
第3页:尺寸A
第4页:尺寸A
第5页:尺寸A
第6页:尺寸B
第7页:尺寸A
然后按照顺序重新开始。

目前,我得到了以下代码,我想在网站的其他页面上重复使用这些代码来显示子页面:

<?php query_posts('post_type=page&order=ASC&post_parent=0');
while(have_posts() ) : the_post(); ?>
   <a href="<?php the_permalink() ?>">
        <?php the_post_thumbnail(); ?>
        <?php the_title(); ?>
   </a>
<?php endwhile;?>

谢谢你的帮助。

这取决于您如何决定,选择哪种方法。关于模式的顺序,通过css进行操作是有意义的。Wordpress也输出像evenodd这样的更改类,但是,由于后面有Three(A A A),所以这不是正确的方法。我认为最简单的是,如果你使用CSS,并使我们的第n种类型(int):

/* First Post Type A */
.your-post-type:first-child {
}
/* Second Post Type B*/
.your-post-type:nth-of-type(2) {
}
/* Third Post Type A */
.your-post-type:nth-of-type(3) {
}
/* Fourth Post Type A */
.your-post-type:nth-of-type(4) {
}
/* Fifth Post Type A */
.your-post-type:nth-of-type(5) {
}
/* Sixth Post Type B */
.your-post-type:nth-of-type(6) {
}
/* Seventh Post Type A */
.your-post-type:nth-of-type(7) {
}

如果你在首页上的自定义查询不限于7,那么你就可以将这个例子扩展到14,或者你有多少帖子想要显示。使用最后一个参数shotposts=X:限制您的查询

<?php query_posts('post_type=page&order=ASC&post_parent=0&showposts=14')..

假设您已经定义了缩略图大小(例如使用add_image_size()),并且您的序列A B A A A B A对我来说非常随机,我会重复迭代一个具有缩略图大小的数组,将其传递给the_post_thumbnail()函数。类似于:

<?php
query_posts('post_type=page&order=ASC&post_parent=0');
$thumbsizes = [
    'size-a',
    'size-b',
    'size-a',
    'size-a',
    'size-a',
    'size-b',
    'size-a',
];
$i = 0;
while (have_posts()) : the_post(); ?>
   <a href="<?php the_permalink() ?>">
        <?php the_post_thumbnail($thumbsizes[ $i ]); ?>
        <?php the_title(); ?>
   </a>
<?php
$i = ($i + 1) % count($thumbsizes);
endwhile;
?>

当然,你的CSS对缩略图大小也有意见