0

感谢这里的一些答案,我设法将我的帖子区分为最新帖子和所有其他帖子。但是,它必须是最旧的帖子。这是我目前的loop

<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
    <div class="inner_box">
        <ul>
            <div class="title">
                <a href="<?php the_permalink();?>">
                    <?php the_title(); ?>
            </div>
        <?php the_content(); ?>
        </a>
        </ul>
        <div class="down">a</div>
    </div>
</div>
<?php else :?>
    <!-- second post -->
<div class="container">
    <div class="inner_box">
        <ul>
            <div class="title">
                <a href="<?php the_permalink();?>">
                    <?php the_title(); ?>
            </div>
        <?php the_content(); ?>
        </a>
        </ul>
        <div class="up">b</div>
    </div>
</div>

<?php endif; ?>`

我在某处看到您可以使用 while 循环来定位 post.length 中的最后一篇文章。但是,我不确定如何实现这一点。

4

1 回答 1

0

你是对的。使用count.

假设帖子总数为 5 个$total_posts = count($posts);
你必须检查你$counter的数组,total - 1因为数组是$posts[0], $posts[1], $posts[2], $posts[3], $posts[4].

<?php 
if ( have_posts() ) : 
    $counter = 0;
    $total_posts = count($posts) - 1; 

    while ( have_posts() ) : 
        the_post(); 
        if( $counter == $total_posts ) {
            ?>
            <div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
            <?php
        } else {
            ?>
            <div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
            <?php
        }
        $counter++;
    endwhile;   
endif;

请注意,您只需要在从 PHP 更改为 HTML 时打开<?php和关闭,在每一行都使用它们是非常混乱的。?>

于 2013-08-21T15:46:03.903 回答