简体   繁体   English

为每第三个wordpress帖子添加一个类

[英]Add a class to every third wordpress post

Im listing all wordpress posts in the blog category but trying to add a class called 'last' to every third 'fourcol' class 我列出了博客类别中的所有wordpress帖子,但试图在每三个'fourcol'类中添加一个名为'last'的类

HTML 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 -->

Hope this makes sense? 希望这有道理吗?

EDIT: 编辑:

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

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

<?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 -->

Try this. 尝试这个。 Should work. 应该管用。

Using CSS 使用CSS

Instead of 代替

.last

Use 采用

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

you need the 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; ?>

You can do that with PHP in your template. 你可以在模板中使用PHP来做到这一点。 Just add the string last on every third post. 只需在每三个帖子上添加last一个字符串。 The following variant makes use of the existing post counter in wordpress and the modulo operator . 以下变体使用wordpress中的现有后置计数器模运算符 The counter starts at 0 so we add 1 to it every time: 计数器从0开始,所以我们每次都加1:

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

This is fairly compact and the most compact solution I can imagine for wordpress on the PHP side of your theme. 这是相当紧凑的,我可以想象在你的主题的PHP方面的wordpress最紧凑的解决方案。

The idea behind it is the following: 其背后的想法如下:

Add a variable as a counter, count it up on every post and if it is 3, set it to 0 again and add the class. 添加一个变量作为计数器,在每个帖子上计数,如果是3,再次将其设置为0并添加该类。

The following script shows each of this step: Define the counter if it does not exists, count it up, reset it to 0 if applicable and do the echo: 以下脚本显示了此步骤:定义计数器(如果不存在),将其计数,如果适用则将其重置为0并执行回显:

<div class="fourcol<?php

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

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

    echo ' last';
  }

 ?>">

That's for demonstration only. 这只是为了演示。 As you're using the standard query object, it's much more easy as we can re-use an existing variable. 当您使用标准查询对象时,我们可以重新使用现有变量。 Additionally making use of the modulo operator helps to find every X element. 另外,使用模运算符有助于找到每个X元素。

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>';

} }

Here add a clrFix div after every 4 post. 这里每4个帖子后添加一个clrFix div。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM