php - 如何以不同于其他帖子的方式显示第一篇帖子?

标签 php html css wordpress

我创建的设计将用于显示与其他帖子不同的第一篇帖子。这并不容易,因为第一个帖子需要在他自己的 div id 中,这与其他帖子完全不同。

这是我目前在 wordpress php 中使用的代码:

<?php get_header(); ?>

<!-- Begin Content -->

<div id="content-wide">
  <div class="post">

    <div class="imgwide" style="background: linear-gradient(to bottom, rgba(0,0,0,0) 60%, rgba(0,0,0,0.8)), url(<?php echo catch_that_image() ?>); background-repeat: no-repeat; background-size: 100%; background-position: center;">

      <div class="p-heading">
        <h1>
          <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
            <?php the_title(); ?>
          </a>
        </h1>
        <div class="p-content">
          Excerpt text of the first post goes here but php the_excerpt doesnt work for this wide paragraph
        </div>
      </div>

    </div>

  </div>
</div>

<div id="content-a">

  <?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
  <div class="post">
    <div class="imgdiv"><a href="<?php the_permalink() ?>"><img src="<?php echo catch_that_image() ?>" width="250"></a></div>
    <div class="p-heading">
      <h1>
        <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
          <?php the_title(); ?>
        </a>
      </h1>
    </div>

    <div class="p-content">
      <?php the_excerpt(); ?>
    </div>

    <div class="p-info">
      <?php the_time('j.m.Y') ?> |
      <?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12">
      <?php $totalcomments = get_comments_number(); echo $totalcomments; ?>
    </div>

  </div>

  <?php endwhile; ?>

这是我的网站网址 http://www.virmodrosti.com/zdravje/ 我想要的只是第一篇文章不会显示两次,而只会移动到宽幅的文章设计中。大帖子在内容范围内。让我知道该怎么做。谢谢。

最佳答案

尝试添加一个从零开始的计数器并在 while 循环中递增它,然后使用 if else 语句检查计数器的值,如果零则显示第一篇文章,否则显示其他文章。

已编辑

    <?php $counter = 0; ?>
    <?php while (have_posts()) : the_post(); ?>
        <?php if($counter == 0) { ?>
            <div id="content-wide">
                <div class="post">
                    <div class="imgwide" style="background: linear-gradient(to bottom, rgba(0,0,0,0) 60%, rgba(0,0,0,0.8)), url(<?php echo catch_that_image(); ?>); background-repeat: no-repeat; background-size: 100%; background-position: center;">
                        <div class="p-heading">
                            <h1><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>
                            <div class="p-content">
                                <?php the_excerpt(); ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        <?php } ?>
        <?php if($counter > 0) { ?>
            <div class="post">
                <div class="imgdiv"><a href="<?php the_permalink() ?>"><img src="<?php echo catch_that_image() ?>" width="250"></a></div>
                <div class="p-heading">
                    <h1><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
                <div class="p-content">
                    <?php the_excerpt(); ?>
                </div>
                <div class="p-info">
                    <?php the_time('j.m.Y') ?> |
                    <?php the_category(', '); ?> | <img src="http://www.virmodrosti.com/wp-content/uploads/comments.png" alt="Komentarji" width="26" height="12">
                    <?php $totalcomments = get_comments_number(); echo $totalcomments; ?>
                </div>
            </div>
        <?php } ?>
        <?php $counter ++; ?>
    <?php endwhile; ?>

关于php - 如何以不同于其他帖子的方式显示第一篇帖子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44571245/

相关文章:

php - 将值添加到数组中

php - 使用特定地址不可更改的打开PayPal支付

javascript - 使用 JavaScript 编辑 CSS

html - 用 Accordion 对齐 Bootstrap

html - 带有 NSString 的 UIWebView 中的 Img Css 百分比

javascript - jQuery .css 不适用于列表中的所有 ID

css - 使用菜单中的填充更改 2 列页面中 body div 的宽度。在右侧获得差距

php - 保护 PHP 站点中的文件

php - 来自 mysql 的 trader_sma 函数的数组

html - 运行时错误 '424' vba 宏需要对象