Reputation: 1112
I have code for my carousel in 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();
?>
I display 5 post in carousel. I want basic If-else statment where for every page I create static variable. for instance:.
if (post == 1) {
$aka = 7;
} else if (post == 2) {
$aka = 8;
} else if (post == 3) {
$aka = 9;
} .. and etc.
I can't figure it out how to implement it in WP, how to say which post is now?
Upvotes: 0
Views: 1964
Reputation: 26
When you are in WP_Query while-loop, you can access current post by $post variable, which has standard post object. Just don't forget to have following row before the loop:
<?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();
?>
Upvotes: 1