Wordpress WP_Query

时间:2013-02-01 21:55:05

标签: wordpress

我在Wordpress中有我的轮播代码

<?php 
    $the_query = new WP_Query(array(
     'category_name' => 'home-slider', 
     'posts_per_page' => 5  
    )); 
    while ( $the_query->have_posts() ) : 
    $the_query->the_post();
?>
<div class="sl-slide">
 <?php the_post_thumbnail('large');?>
 <div class="sl-slide-inner">
 <?php the_title();?>
 <?php the_excerpt();?>
</div>
</div>
<?php 
endwhile; 
wp_reset_postdata();
?>

我在旋转木马上显示5个帖子。我想要基本的If-else语句,我为每个页面创建静态变量。例如:。

if (post == 1) {
 $aka = 7;
} else if (post == 2) {
 $aka = 8;
} else if (post == 3) {
 $aka = 9;
} .. and etc.

我无法弄清楚如何在WP中实现它,怎么说现在哪个帖子?

1 个答案:

答案 0 :(得分:1)

当您在WP_Query while循环中时,您可以通过$ post变量访问当前帖子,该变量具有标准的post对象。 只是不要忘记在循环之前有以下行:

<?php 
    global $post;
    $the_query = new WP_Query(array(
     'category_name' => 'home-slider', 
     'posts_per_page' => 5  
    )); 
    while ( $the_query->have_posts() ) : 
    $the_query->the_post();
?>
<div class="sl-slide">
<?php
if ($post->ID == 1) {
 $aka = 7;
} else if ($post->ID == 2) {
 $aka = 8;
} else if ($post->ID == 3) {
 $aka = 9;
}
?>
 <?php the_post_thumbnail('large');?>
 <div class="sl-slide-inner">
 <?php the_title();?>
 <?php the_excerpt();?>
</div>
</div>
<?php 
endwhile; 
wp_reset_postdata();
?>