PHP计数器:输出列表中的反向数字
实际上,我有一个PHP循环,我给每个结果一个数字,从1开始,然后按升序排列。结果如下: 1) 第C条PHP计数器:输出列表中的反向数字,php,wordpress,Php,Wordpress,实际上,我有一个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> &
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;
?>
有没有一个简单的方法可以做到这一点?
非常感谢,如果有一个函数返回posts count,例如
count\u 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;
?>
如果有一个函数返回posts count,例如
count\u 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功能相关的帖子: 您可能会更好地使用类似于查询的帖子: 哪一个能让你们更好地控制帖子的显示 编辑: 或者,如果您使用此选项:
$count_posts = wp_count_posts();
您可以通过反转计数器($counter--;)将其与其他答案结合使用
这应该能奏效
如果这是一个基于wordpress的站点/页面,并且该功能具有与wordpress功能相关的帖子: 您可能会更好地使用类似于查询的帖子: 哪一个能让你们更好地控制帖子的显示 编辑: 或者,如果您使用此选项:
$count_posts = wp_count_posts();
您可以通过反转计数器($counter--;)将其与其他答案结合使用
这应该能奏效
因此,您的代码可以变成:
<?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;
?>
因此,您的代码可以变成:
<?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;
?>
在帖子数量处启动$计数器
,然后执行$计数器--
?显然:在最大数字处启动计数器,然后倒计时,而不是倒计时。所以问题归结为:如何找到你有多少帖子?首先,我要查阅Wordpress文档。如果您使用的是HTML5,请使用一个具有相反属性的有序列表:在帖子数量处启动$counter
,然后执行$counter--
?显然:在最大数量处启动计数器,然后倒计时,而不是倒计时。所以问题归结为:如何找到你有多少帖子?首先,我要查阅Wordpress文档。如果你使用HTML5,请使用一个具有反向属性的有序列表:wp_count_posts将再次运行查询,因此效率低下。wp_count_posts将再次运行查询,因此效率低下。令人赞叹的快速答案。。。你们是可怕的家伙。谢谢你,2012年使用了global
?非常棒的快速答案。。。你们是可怕的家伙。谢谢你,2012年使用global
?