【问题标题】:Wordpress: Style first post differently from the restWordpress:第一篇文章的风格与其他文章不同
【发布时间】:2016-04-05 18:45:18
【问题描述】:

我一直在四处寻找,但找不到合适的解决方案。我的目标是使第一篇文章的样式与其他文章不同。我把它们放在一个 3*3 的网格中,我想先张贴为全角。

以下代码在 index.php 中生成循环。

<ul class="list_post">

            <?php $i = 1; ?>
            <?php if (have_posts()) : while (have_posts()) : the_post(); ?>                    

                <?php get_template_part('content','grid'); ?>                                          
                <?php if ($i%3 == 0) : ?>
                    <div class="clearfix"></div>
                <?php endif; $i++; ?>

            <?php endwhile; wp_reset_query(); endif; ?> 

</ul>

呈现的 HTML 输出是:

<ul class="list_post">
  <li>
    <article id="post-7375" class="post_grid post-7375 post type-post status-publish format-video has-post-thumbnail hentry post_format-post-format-video">
      <div class="post-img"></div>
      <div class="post-header"></div>
      <div class="post-entry"></div>
    </article>
  </li>
  <li>(..)</li>
  <li>(..)</li>
</ul>

我不确定如何做到这一点?

【问题讨论】:

  • 使用 CSS。 ul.list_post li:first-child{...}
  • 好建议,但那行不通。它不会触发第一个:post。
  • 我不明白你的意思是“它不会触发第一个:post”。渲染出来的 HTML 是什么样子的?
  • 但是您的代码似乎是您试图在每 3 个帖子之后放置 clearfix。您的问题不清楚,为什么不尝试if ($i == 1) { //This is first post } 并且在第一次发布后 $i 将大于 1
  • 抱歉不清楚,在这里更新了第一篇文章,包含 html 输出和更多信息。

标签: php wordpress post


【解决方案1】:

content-grid.php 上更改前几行以反映以下内容:

global $i;

<article id="post-<?php the_ID(); ?>" <?php post_class('article-num-' . $i); ?>>

然后在你的 CSS 中,你可以这样做:

.article-num-1{
..
}

【讨论】: