<?php if ( have_posts() ) : ?>
<?php
$count = 1;
$featuredPosts = 9;
query_posts('showposts=19');
?>
<div class="articleTile column_3">
<?php while ( have_posts() ) : the_post(); ?>
<?php if($count > $featuredPosts) : ?>
<!--change class to articleList column_2-->
<?php endif; ?>
<div class="column">
<?php get_template_part( 'content', 'featured' ); ?>
<!-- #post-<?php the_ID(); ?> -->
</div>
<?php
$count = $count ++;
endwhile;
?>
</div>
当count变量达到10时,我如何定位<div class="articleTile column_3">
元素并更改其类?
我设法做到了:
<?php if ( have_posts() ) : ?>
<?php
$count = 1;
$featuredPosts = 9;
query_posts('showposts=19');
?>
<div class="articleTile column_3">
<?php while ( have_posts() ) : the_post(); ?>
<?php if($count == $featuredPosts + 1) : ?>
<?php echo $count ?>
</div>
<div class="articleList column_2">
<?php endif; ?>
<div class="column">
<?php echo $count ?>
<?php get_template_part( 'content', 'featured' ); ?>
<!-- #post-<?php the_ID(); ?> -->
</div>
<?php
$count = $count +1;
endwhile;
?>
</div>
如果没有具有不同类别的第二个元素,我的原始方式将无法工作。我很抱歉。感谢他们的快速反应,他们帮助我指明了正确的方向。
答案 0 :(得分:2)
首先运行一个循环以检查总计数是多少然后用它来设置正确的类是不是更聪明?
也许这样的事情? :
<?php if ( have_posts() ) : ?>
<?php
$count = 1;
$count_posts = 1;
$featuredPosts = 9;
$class = 'column_3';
query_posts('showposts=19');
/* Run loop */
while ( have_posts() ) : the_post();
$count_posts = $count_posts ++;
endwhile;
if ($count_posts > $featuredPosts) { $class = 'column_2'; }
?>
<div class="articleTile <? echo $class; ?>">
<?php while ( have_posts() ) : the_post(); ?>
<div class="column">
<?php get_template_part( 'content', 'featured' ); ?>
<!-- #post-<?php the_ID(); ?> -->
</div>
<?php
$count = $count ++;
endwhile;
?>
</div>
答案 1 :(得分:0)
您可以在div类中添加一个条件,如
你需要把这个div放在循环中
<div class="<?php echo ($count<=10) ? 'articleTile column_3' : 'articleTile column_5' ?>">
答案 2 :(得分:0)
这就是我使用的:
<?php if ( have_posts() ) : ?>
<?php
$count = 1;
$featuredPosts = 9;
query_posts('showposts=19');
?>
<div class="articleTile column_3">
<?php while ( have_posts() ) : the_post(); ?>
<?php if($count == $featuredPosts + 1) : ?>
<?php echo $count ?>
</div>
<div class="articleList column_2">
<?php endif; ?>
<div class="column">
<?php echo $count ?>
<?php get_template_part( 'content', 'featured' ); ?>
<!-- #post-<?php the_ID(); ?> -->
</div>
<?php
$count = $count +1;
endwhile;
?>
</div>
答案 3 :(得分:-1)
<div class=<?php echo $class; ?></div>
您可以在要更改类的元素上尝试此操作。
$class = "oldclass";
if(a==10){
$class= "newclass";
}