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

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

<div class="articleTile column_3">当 count 变量达到 10 时,我将如何定位元素并更改其类?

编辑:对不起,我是个白痴。

我已经设法做到了:

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

如果没有具有不同类的第二个元素,我的原始方法无论如何都不会起作用。我很抱歉。感谢他们帮助我指出正确方向的快速反应。

4

4 回答 4

2

首先运行 a 循环以检查总计数然后使用它来设置正确的类不是更聪明吗?

大概是这样的?:

<?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>
于 2012-04-26T09:15:46.257 回答
0

您可以在 div 类中添加一个条件,例如

你需要把这个 div 放在一个循环中

<div class="<?php echo ($count<=10) ? 'articleTile column_3' : 'articleTile column_5' ?>">
于 2012-04-26T09:14:13.107 回答
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>
于 2012-10-11T08:26:55.640 回答
-1
<div class=<?php echo $class; ?></div>

您可以在要更改类的元素上尝试此操作。

$class = "oldclass";
if(a==10){
$class= "newclass";
}
于 2012-04-26T09:18:39.127 回答