如何显示与其他帖子不同的wordpress第一篇文章



索引的第一部分.php,目前有大的帖子显示包含以下条目:

<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>

存档的第一部分.php包含以下条目:

 <?php get_header(); ?>
<!-- Begin Content -->
<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; ?>

我使用不同样式的div id="content" 表示大帖子,div id="content-a" 表示较小的帖子显示,连续 3 个。

现在,我希望只有最新的帖子才能以大格式显示,如 css 中的 #content 定义,其余的就像它们在存档中一样.php带有 #content-a。我该怎么做?

我的网站主索引页已 http://www.virmodrosti.com/,存档在此处 http://www.virmodrosti.com/zdravje/

请告诉我,谢谢。

绝对

最简单的方法是在 CSS 中使用 ::first-child 伪选择器。

如果这不是一个选项,则可以向while循环添加一个计数器,并使用它来向项目添加类。

<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php 
  $counter = 0;
  if (have_posts()) : 
 ?>
  <?php while (have_posts()) : the_post(); ?>
  <div class="post count-<?php echo $counter; ?>">
  <div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();        ?></a></h1></div>
  <div class="p-content">
  <?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
 </div>
 <?php 
  $counter++;
  endwhile; ?>

你的 css 选择器有两个问题。

1 - 您不能使用div id='content-a' 来设置多个帖子的样式,因为 id 是唯一的。您必须使用 class=content-a

2 - 您的帖子循环if (have_posts()) : while (have_posts()) : the_post();内没有id='content'。唯一的id=content是在循环之外。无论如何,它都将应用于所有帖子。

解决方法是使用循环内的类。在您的代码中,最好的代码是post这是更高的div 类。

然后你需要使用 while (have_posts()) 循环来标记第一个帖子......

索引.php

<?php get_header(); ?>
    <!-- Begin Content -->
    <div id="content">
<?php if (have_posts()) : ?>
<?php 
$first_post = true;
while (have_posts()) : the_post(); ?>
    <div class="<?php
     if ($first_post){ 
         $first_post = false; 
         echo 'post-first';
     }else{ 
         echo 'post';
     }
     ?>">
    <div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>

在存档中.php我不明白为什么您在 if (have_posts()) 之后创建一个新的WP_Query对象。但由于它不是问题的一部分,也不是问题的一部分,所以我就这样离开了......

存档.php

<?php get_header(); ?>
<!-- Begin Content -->
<div id="content">
<?php if (have_posts()) : ?>
<?php 
$query = new WP_Query(array('posts_per_page'=> 2,)); 
$first_post = true;
while ($query->have_posts()) : $query->the_post(); ?>
    <div class="<?php
     if ($first_post){ 
         $first_post = false; 
         echo 'post-first';
     }else{ 
         echo 'post';
     }
     ?>">    <div class="p-heading"><h1><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1></div>
<div class="p-content">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>
<div class="p-info"><?php the_time('j.m.Y') ?> | <?php the_category(', '); ?> | <?php comments_popup_link('Ni komentarjev', 'En komentar', '% komentarjev'); ?></div>
</div>
<?php endwhile; ?>

最新更新