2009-09-03 42 views
1

我有一个简单的查询:$query = new WP_Query('showposts=5');显然会显示5最新的职位。有什么办法可以在查询中获得帖子的位置吗?按位置我的意思是... $查询有5个职位,我需要能够显示循环中的职位数量。我真的不知道PHP,但我假设$查询是一个数组变量(?)与这5个职位。如何在查询中显示帖子的“位置”?

这会在JavaScript滑块的事情,其中​​的每一个岗位我显示像<a href="#1"></a>链接使用,我需要数为2的第二个职位,3为第三等

希望这是任何意义和某人将能够帮助我。

由于提前, 海宁

回答

0

这并不难(我复制cpharmston的PHP代码):

<?php 

// Create the Query 
$query = new WP_Query('showposts=5'); 

if ($query->have_posts()) : 

    $i = 1; // for counting 

    // Create the Javascript slider thing 
    while ($query->have_posts()) : $query->the_post(); 
     // Do stuff here 
     $i++; // make sure this is @ the end of the while-loop 
    endwhile; 


    $i = 1; // reset $i to 1 for counting 

    // Display the posts 
    while ($query->have_posts()) : $query->the_post(); 
     // Do stuff here 
     $i++; // make sure this is @ the end of the while-loop 
    endwhile; 

endif; 

?> 

你可以使用$ i#1,#2等。每当while循环结束时,$ i ++会确保它以1递增(因此在第一次$ i = 2之后等)。

希望这有助于:)(我认为,虽然cpharmston的解决方案会更容易)。

+0

这正是我一直在寻找的。非常感谢! – Justine 2009-09-03 13:29:21

3

更多防弹的行为,我想,而不是他们的页面上的位置创建一个使用每个岗位的UID(使用the_ID())的锚链接。

此外,您应该通过$query迭代使用the loop,您可以在页面中多次执行此操作。 (有没有在使用了一段时间的WordPress,该代码可能有点过,但概念是合理的)

<?php 

// Create the Query 
$query = new WP_Query('showposts=5'); 

if ($query->have_posts()) : 

    // Create the Javascript slider thing 
    while ($query->have_posts()) : $query->the_post(); 
     // Do stuff here 
    endwhile; 

    // Display the posts 
    while ($query->have_posts()) : $query->the_post(); 
     // Do stuff here 
    endwhile; 

endif; 

?> 
1

$ query-> current_post将为您提供循环中当前项目的索引。另外,$ query-> post_count为您提供了循环中项目的总数。这也可能有帮助。

相关问题