Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查找最早的帖子—;WordPress_Php_Wordpress_Wordpress Theming - Fatal编程技术网

Php 查找最早的帖子—;WordPress

Php 查找最早的帖子—;WordPress,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,多亏了这里的一些答案,我成功地将我的帖子区分为最新的帖子和所有其他帖子。但是,它必须是最古老的帖子。这是我当前的循环: <?php if (have_posts()) : ?> <?php $post = $posts[0]; $c=0;?> <?php while (have_posts()) : the_post(); ?> <!-- first post --> <?php $c++; if( $c == 1) :?> <d

多亏了这里的一些答案,我成功地将我的帖子区分为最新的帖子和所有其他帖子。但是,它必须是最古老的帖子。这是我当前的
循环

<?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; ?>`

A.
B `
我在某个地方看到,可以使用while循环从post.length指向最后一篇文章。但是,我不确定如何实现这一点。

是的,你是对的。使用

假设
$total\u posts=count($posts)的总posts为5
您必须检查
$counter
中的
总计-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时,在每一行使用它们都会非常混乱。

谢谢。如果我使用这种方法,我会把我的内容放在哪里?(
等)用基本知识更新了答案,只需调整它以匹配您的原始代码。您需要只设置最旧帖子的样式,还是只设置最旧帖子及其前面的帖子的样式?本质上,如果只有10篇文章(10篇是最老的ID),你想检索10篇{特殊样式}然后是9、8、7等等{正常样式}还是只想要10篇?