我实际上有一个PHP循环,我给每个结果一个数字,从1开始,然后按升序排列。输出如下:
1) C
条2) B
条3) A
条
。但我想反转列表编号,所以我得到类似的东西:
3)文章C(文章的顺序不会改变,它们是按日期降序排列的)
2) B
条1) A
条
这是我当前的循环:
<?php
if (have_posts()) :
$counter = 1;
while (have_posts()) :
the_post(); ?>
<div>
<span class="count"><?php echo $counter; ?></span>
<?php the_title(); ?>
</div>
<?php
$counter++;
endwhile;
endif;
?>
有没有简单的方法可以做到这一点?非常感谢,
WP_Query对象有一个保存帖子数的变量:
$query->post_count
因此,您的代码可以变为:
<?php
if (have_posts()) :
global $wp_query;
$counter = $wp_query->post_count;
while (have_posts()) :
the_post(); ?>
<div>
<span class="count"><?php echo $counter; ?></span>
<?php the_title(); ?>
</div>
<?php
--$counter;
endwhile;
endif;
?>
如果有一个函数返回帖子计数,例如 count_posts()
(只是猜测),以这种方式使用它:
<?php
if (have_posts()) :
$counter = wp_count_posts();
while (have_posts()) :
the_post(); ?>
<div>
<span class="count"><?php echo $counter; ?></span>
<?php the_title(); ?>
</div>
<?php
$counter--;
endwhile;
endif;
?>
如果这是一个基于 wordpress 的网站/页面,并且该函数有与 wordpress 函数相关的帖子:
你最好是这样query_posts:http://codex.wordpress.org/Function_Reference/query_posts
哪个可以让您更好地控制帖子的显示?
编辑:或者,如果您使用它:
$count_posts = wp_count_posts();
您可以通过反转计数器($counter--;)将其与其他答案结合使用
这应该可以解决问题
http://codex.wordpress.org/Function_Reference/wp_count_posts