Wordpress-如果是第一篇文章


Wordpress - If the first post

我用这个代码来显示我的10篇主页帖子:

<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

我想以某种方式识别第一个帖子,并仅更改第一个帖子的代码,例如,第一篇文章应该显示_except,而不是像下面这样的标题:

<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

使用一个变量来计算循环通过的帖子数。如果计数器为0,则您处于第一个帖子:

<?
query_posts('category_name=homepage&showposts=10&order=DESC');
$i = 0;
while ( have_posts() ) : the_post(); 
    $i == 0 ? the_excerpt() : the_title(); 
    the_content(); 
    $i++;
endwhile;
?>

您可以设置一个计数器,使每个循环递增。

通过1 启动

$count = 1;

每个循环执行$count++;

if ($count ==1){the_excerpt();} else{the_content();}