【发布时间】:2018-12-15 18:33:52
【问题描述】:
情况:在我的 WordPress 网站上,我正在我的存档 PHP 模板上输出帖子,我想每隔一个 pair(在第一个帖子之后)分配一个 CSS 类“苗条”,让剩下的人得到“宽”的班级。此外,我希望最后一篇文章获得“完整”类。
问题:我无法编写代码,因此我可以为每一对分配“slim”类。
这是一个快速的drawing of the layout
我当前的代码大部分都可以正常工作,只是不确定如何为这些对分配类。到目前为止,这是我的代码:
<section id="primary" class="content-area">
<main id="main" class="site-main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php the_archive_title( '<h1 class="page-title">', '</h1>' ); ?>
</header><!-- .page-header -->
<?php
// Start the Loop counter:
$client_post_index = 0;
while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php
// check if a new row is needed.
if ( $client_post_index % 2 == 0 ) : ?>
<div class="work-archive-row">
<?php endif ?>
<?php
// check if the entry is odd or even and assign wide or slim class.
if ( $client_post_index % 3 == 0 ) : ?>
<div id="post-<?php the_ID(); ?>" class='work-archive-post-wide'>
<?php twentynineteen_post_thumbnail(); ?>
</div>
<?php else: ?>
<div id="post-<?php the_ID(); ?>" class='work-archive-post-slim'>
<?php twentynineteen_post_thumbnail(); ?>
</div>
<?php endif ?>
<?php $client_post_index++; ?>
<?php if ( $client_post_index % 2 == 0 ) : ?>
</div>
<?php endif ?>
<?php endwhile;
// Previous/next page navigation.
twentynineteen_the_posts_navigation();
// If no content, include the "No posts found" template.
else :
get_template_part( 'template-parts/content/content', 'none' );
endif;
?>
</main><!-- #main -->
</section><!-- #primary -->
用到的CSS如下:
.work-archive-row {
height: 500px;
margin: 16px calc(5% + 60px) 8px calc(5% + 60px); }
.work-archive-row .work-archive-post-odd {
max-width: calc( 65% - 16px);
width: 100%;
margin: 0 8px;
float: left;
height: 500px;}
.work-archive-row .work-archive-post-even {
width: calc( 35% - 16px);
float: left;
height: 500px;
margin: 0 8px;}
.work-archive-row .work-archive-post-odd img {
height: 500px;
object-fit: cover;}
.work-archive-row .work-archive-post-even img {
height: 500px;
object-fit: cover;}
更新: 另一个例子 - 假设我的存档循环吐出 10 个帖子,那么我需要为帖子分配如下类别:
- 帖子 01:宽
- 后 02:苗条
- 后 03:苗条
- 发布 04:宽
- 05 后:宽
- 06后:苗条
- 07后:苗条
- 发布 08:宽
- 09 后:宽
- 帖子 10:已满
正如您从所附图片中看到的,每行将只包含两个条目,但我需要更改大小(CSS 类)。
任何指向该类型循环迭代的指针或引用将不胜感激。提前谢谢你。
【问题讨论】:
-
你似乎是除以 3 并使用余数,我想你想检查
$client_post_index % 2 == false -
然后看wordpress.stackexchange.com/questions/25591/… 这解释了如何检测最后一个帖子
-
@simon.ro ace 感谢您的链接 :)