2014-12-24 34 views
0

好的,我创建了一个投资组合页面,我想添加一个类到我的上一个投资组合缩略图。目前我有一个课程添加到最后一个投资组合项目的分部。我想将它添加到缩略图中,而不是div本身。如何添加类到循环中的最后一篇文章的最后一个图像[Wordpress]

我的代码:

<?php $args = array('post_type' => 'portfolio', 'order' => 'ASC'); 
$loop = new WP_Query($args); 
while ($loop->have_posts()) : $loop->the_post(); ?> 
<div class="testing_imran<?php echo $loop->current_post + 1 === $loop->post_count ? ' imd_last' : '' ?>"> 
<?php the_post_thumbnail('thumbnail', array('class' => 'main-image portfolio')); ?> 
</div>   
<?php endwhile; ?> 

我想要的“imd_last”类添加到缩略图后不股利。我该怎么做?顺便说一下,缩略图有一个附加的类,你可以看到,我只想添加一个额外的类到最后一个缩略图。 enter image description here

+0

是你上面的代码中添加此类“imd_last”到最后的缩略图div? – Zeeshan

+0

是的,它的确如此。 &我希望这个类添加到缩略图,而不是div。附上截图。 –

+0

请检查我的回答 – Zeeshan

回答

0

试试这个,

<?php $args = array('post_type' => 'portfolio', 'order' => 'ASC'); 
$loop = new WP_Query($args); 
while ($loop->have_posts()) : $loop->the_post(); ?> 
<?php $extraLastClass = $loop->current_post + 1 === $loop->post_count ? ' imd_last' : '';?> 
<div class="testing_imran"> 
<?php the_post_thumbnail("thumbnail", array("class" => "main-image portfolio $extraLastClass")); ?> 
</div> 
<?php endwhile; ?> 
+0

哇,变化很简单。很棒的工作,非常感谢你。 –

+0

它是我的荣幸@Aohor Arsalan – Zeeshan

0

如果妳想要做到这一点,u可以使用CSS:最后一个孩子 或使用jQuery并添加动态的最后一节课为$("div").last().addClacss("myclass");

相关问题