2011-07-27 35 views
2

林列出所有WordPress的帖子在博客类别,但尝试了一个名为“最后”类添加到每三个“fourcol”类类添加到每三个WordPress邮寄

HTML

<div class="container"> 
    <div class="row"> 

    <?php query_posts('category_name=blog&showposts=10&orderby=date&order=dsc'); if (have_posts()) : while (have_posts()) : the_post(); ?> 

     <div class="fourcol"> 
      <a href=""><h2 class="blogtitle"><?php the_title(); ?></h2></a> 
      <a href="#"><img src="images/_scroll1.jpg"></a> 
      <span class="date">12 May 2011</span> 
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> 
      <a class="more" href="#">Keep reading</a> 
       </div><!-- fourcol END --> 

      <?php endwhile; endif; ?> 
     </div><!-- row END --> 
    </div><!-- container END --> 

希望这有意义吗?

+0

为你更新了我的答案。误读它 – Phil

回答

3

使用CSS而不是

.last 

使用

.fourcol:nth-child(3n+1) 
+0

这个效果很好,但是理想的情况是想将它设置为一个序列,即:类'last'将应用于帖子3,6,9,12等 – Rob

+0

那么将是第n孩子(3n + 1)编辑 – ChristopheCVB

+0

那么Internet Explorer呢? –

3

编辑:

var i = 1; 
$('#row .fourcol').each(function() { 
    if(i++ % 4 == 0) 
     $(this).addClass('last'); 
}); 

$('.fourcol').eq(3).addClass('last'); 
+0

你击败了我。 +1 那里不会是一个更容易的方法 –

+0

你为什么要用JavaScript来做到这一点? – locrizak

+0

@locrizak:看看这个问题标签。 – hakre

2

您需要的modulus operator

<?php query_posts('category_name=blog&showposts=10&orderby=date&order=dsc'); 
    $counter = 0; //add a ounter to keep track of the number post we're on 
    if (have_posts()) : while (have_posts()) : the_post(); ?> 
    //check if the remainder of $count is 0, if so add the class 'last' 
    <div class="fourcol <? if ($count % 3 == 0) echo 'last' ?>">               
     <a href=""><h2 class="blogtitle"><?php the_title(); ?></h2></a> 
     <a href="#"><img src="images/_scroll1.jpg"></a> 
     <span class="date">12 May 2011</span> 
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> 
     <a class="more" href="#">Keep reading</a> 
    </div><!-- fourcol END --> 
    <? $count++; ?> 
<?php endwhile; endif; ?> 
+0

伟大的作品,但增加了类的第一篇文章,而不是第三,任何想法? – Rob

+0

不好意思更新了''counter'应该从0开始而不是1 – locrizak

3

<?php query_posts('category_name=blog&showposts=10&orderby=date&order=dsc'); 
if (have_posts()) : 
    $i=0; 
    while (have_posts()) : 
     the_post(); 
    ++$i; 
    ?> 

    <div class="fourcol<?php if($i%3==0) echo ' every-third-post' ?>" > 
     <a href=""><h2 class="blogtitle"><?php the_title(); ?></h2></a> 
     <a href="#"><img src="images/_scroll1.jpg"></a> 
     <span class="date">12 May 2011</span> 
     <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p> 
     <a class="more" href="#">Keep reading</a> 
      </div><!-- fourcol END --> 

     <?php endwhile; endif; ?> 
    </div><!-- row END --> 
</div><!-- container END --> 

试试这个。应该管用。

1

你可以在你的模板中用PHP来做到这一点。每隔三个帖子只需添加字符串last。以下变体使用existing post counter in wordpressmodulo operator。计数器从0开始,所以我们每次对它加1:

<div class="fourcol<?php if (!((1 + $wp_query->current_post) % 3)) echo ' last' ?>"> 

这是相当紧凑和最紧凑的解决方案,我能想到的WordPress您的主题的PHP端。

它背后的想法是这样的:

添加一个变量作为计数器,算它的每一个岗位,如果是3,它再次被设置为0,并添加类。

下面的脚本显示了每个这一步:定义计数器,如果不存在的话,指望它时,它重置为0,如果适用,并做回声:

<div class="fourcol<?php 

    isset($iposts) || $iposts = 0; 

    if (++$iposts === 3) 
    { 
    $iposts = 0; 

    echo ' last'; 
    } 

?>"> 

这是仅用于演示。在使用标准查询对象时,由于我们可以重新使用现有变量,因此更容易。另外使用modulo operator有助于查找每个X元素。

0
if (have_posts()) : while(have_posts()) : the_post(); 
//Loop code goes here. 
if (0 == $count%4) { 
    echo 'div class="clrFix"></div>'; 
} 
endwhile; 
if (0 != $count%4) { 
    echo 'div class="clrFix"></div>'; 

}

这里以后每4后添加clrFix股利。