【问题标题】:How to show first post differently from other posts?如何以不同于其他帖子的方式显示第一篇帖子?
【发布时间】:2017-06-15 15:18:01
【问题描述】:

我创建的设计将用于显示与其他帖子不同的第一个帖子。没那么容易,因为第一个帖子需要在自己的 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/ 我想要的只是第一个帖子不会显示两次,而只是移动到宽帖子设计。大帖子在内容范围内。让我知道该怎么做。谢谢。

【问题讨论】:

  • 你只需要一个简单的if声明
  • 我不熟悉 WordPress 和所涉及的 PHP 方法,但似乎如果您可以尝试在您的 while 循环中添加一些类似于 &lt;?php if( this_post != first_post ) : ?&gt; [POST CONTENTS HERE] &lt;?php endif; ?&gt; 的内容。 (换句话说,添加一个if 语句,如@cmorrissey 建议的)
  • 尝试了很多东西,但都不管用。

标签: php html css wordpress


【解决方案1】:

尝试添加一个从零开始的计数器并在您的 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; ?>

【讨论】:

  • 我试过了,收到空白页,不知道为什么。我完全按照规定做了。我不知道第一个帖子的两个不同的 div id 和其余帖子是否相互争斗,或者因为 php if 语句位于没有 html 条目或其他内容的 div id 之前。即使我添加另一个 id 包装器以使代码位于包装器内,它也不会显示第一篇文章,而只会显示所有内容。没有此条目&lt;?php if (have_posts()) : ?&gt; 页面不会加载,只会返回空页面。
  • 是否已将您的 html 包含在 if else 语句中?
  • 是的,它们包含在适用于 virmodrosti.com/zdravje/ 的 archive.php 中
  • 请在您的 wp-config 文件上启用错误报告,以便我们可以看到导致它的错误
  • error_reporting(E_ALL); ini_set('display_errors', 1);定义('WP_DEBUG', true);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-01
  • 1970-01-01
  • 2013-07-20
相关资源
最近更新 更多