duangang79177 2017-06-15 15:18
浏览 40
已采纳

如何以与其他帖子不同的方式显示第一篇帖子

I created the design that will be used to show first post differently from the others. It's not that easy because the first post needs to be in his own div id, which is completely different from the other posts.

Here is the code I currently use in 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; ?>

and here is my site url http://www.virmodrosti.com/zdravje/ All I want is that first post isn't displayed twice, but is only moved to the wide post design. The big post is in content-wide. Let me know how to do that. Thank you.

  • 写回答

1条回答 默认 最新

  • duanfuchi7236 2017-06-15 23:06
    关注

    try to add a counter that starts in zero and increment it inside your while loop, then use if else statement to check the value of counter if zero display the first post else the other posts.

    EDITED

        <?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; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题