我想在页面上的每三个post显示中添加一个css类。我已经尝试了下面给出的代码:
<?php if (have_posts()) : ?>
<?php $c = 0;
while (have_posts()) : the_post(); $c++;
if( $c == 3) {
$style = 'third';
$c = 0;
}
else $style='';
?>
<div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">
我从哪里得到的,不记得在哪里,所以它不是我的,但我用过它,它的工作原理很有魅力,因为它适用于任何页面、档案等:
<div <?php post_class( 0 === ++$GLOBALS['wpdb']->wpse_post_counter % 3 ? 'third' : '' ); ?>>
然而,假设你使用的是默认的.post类,那么你可以很容易地通过CSS 定位
.post:nth-child(3){your code}
或者你的帖子使用的任何类别
尝试更改
while (have_posts()) : the_post(); $c++;
if( $c == 3) {
$style = 'third';
$c = 0;
}
else $style='';
带有
while (have_posts()) : the_post();
if( $c % 3 == 0 )
$style = 'third';
else
$style = '';
并在while循环结束之前使可变CCD_ 1递增
我感觉
<?php post_class($style) ?> id="post-<?php the_ID(); ?>">
正在工作,如果不将其更改为
<?php class = "<?php $style; ?>" id="post-<?php the_ID(); ?>">
您不需要更改任何.php
文件或function
。您只需通过CSS即可实现这一点。
#content div:nth-child(3n)
{
background-color: yellow;
}
注意:只需使用post-div或类选择器更改#content div
即可,一切就绪!
这是演示:jsFiddle
希望它能有所帮助!