Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php While循环:每秒钟输出不同的结果_Php_Wordpress_Plugins_While Loop - Fatal编程技术网

Php While循环:每秒钟输出不同的结果

Php While循环:每秒钟输出不同的结果,php,wordpress,plugins,while-loop,Php,Wordpress,Plugins,While Loop,我正在为WordPress运行一个名为Category Posts Widget的插件: 它使用while循环来显示特定类别中所有帖子的名称。我想得到它,以便在每秒钟的输出上都有一个不同的类附加到li标记 以下是插件的代码块: // Post list echo "<ul>\n"; while ( $cat_posts->have_posts() ) { $cat_posts->the_post(); ?>

我正在为WordPress运行一个名为Category Posts Widget的插件:

它使用while循环来显示特定类别中所有帖子的名称。我想得到它,以便在每秒钟的输出上都有一个不同的类附加到li标记

以下是插件的代码块:

// Post list
    echo "<ul>\n";

    while ( $cat_posts->have_posts() )
    {
        $cat_posts->the_post();
    ?>
        <li class="cat-post-item">
            <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

            <?php
                if (
                    function_exists('the_post_thumbnail') &&
                    current_theme_supports("post-thumbnails") &&
                    $instance["thumb"] &&
                    has_post_thumbnail()
                ) :
            ?>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
                </a>
            <?php endif; ?>

            <?php if ( $instance['date'] ) : ?>
            <p class="post-date"><?php the_time("j M Y"); ?></p>
            <?php endif; ?>

            <?php if ( $instance['excerpt'] ) : ?>
            <?php the_excerpt(); ?> 
            <?php endif; ?>

            <?php if ( $instance['comment_num'] ) : ?>
            <p class="comment-num">(<?php comments_number(); ?>)</p>
            <?php endif; ?>
        </li>
    <?php
    }

    echo "</ul>\n";
我只是想让它在输出列表中的每一秒,li都有一个不同的类,比如cat post item alt

谢谢


韦德

以下内容未经测试,但它说明了基本原理。只需在循环的每个实例上切换一个布尔值。偶数文章还将增加类cat post item偶数,这样您就不必对样式表进行过多的修改

我们很容易发现for循环可以用于简单增量以外的其他用途

这两个编辑的行被标记为这样

// Post list
echo "<ul>\n";

/* edited */    for($even=false;$cat_posts->have_posts();$even=!$even)
{
    $cat_posts->the_post();
?>
<!-- edited -->        <li class="cat-post-item<?php if($even) echo " cat-post-item-even"; ?>">
        <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

        <?php
            if (
                function_exists('the_post_thumbnail') &&
                current_theme_supports("post-thumbnails") &&
                $instance["thumb"] &&
                has_post_thumbnail()
            ) :
        ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
            </a>
        <?php endif; ?>

        <?php if ( $instance['date'] ) : ?>
        <p class="post-date"><?php the_time("j M Y"); ?></p>
        <?php endif; ?>

        <?php if ( $instance['excerpt'] ) : ?>
        <?php the_excerpt(); ?> 
        <?php endif; ?>

        <?php if ( $instance['comment_num'] ) : ?>
        <p class="comment-num">(<?php comments_number(); ?>)</p>
        <?php endif; ?>
    </li>
<?php
}

echo "</ul>\n";

你说得对,我误解了这个问题;他以为他是001001。。反对010101。。其他方式:if$counter&1{}
// Post list
echo "<ul>\n";

/* edited */    for($even=false;$cat_posts->have_posts();$even=!$even)
{
    $cat_posts->the_post();
?>
<!-- edited -->        <li class="cat-post-item<?php if($even) echo " cat-post-item-even"; ?>">
        <a class="post-title" href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

        <?php
            if (
                function_exists('the_post_thumbnail') &&
                current_theme_supports("post-thumbnails") &&
                $instance["thumb"] &&
                has_post_thumbnail()
            ) :
        ?>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
            <?php the_post_thumbnail( 'cat_post_thumb_size'.$this->id ); ?>
            </a>
        <?php endif; ?>

        <?php if ( $instance['date'] ) : ?>
        <p class="post-date"><?php the_time("j M Y"); ?></p>
        <?php endif; ?>

        <?php if ( $instance['excerpt'] ) : ?>
        <?php the_excerpt(); ?> 
        <?php endif; ?>

        <?php if ( $instance['comment_num'] ) : ?>
        <p class="comment-num">(<?php comments_number(); ?>)</p>
        <?php endif; ?>
    </li>
<?php
}

echo "</ul>\n";
$counter=1;

for ($i=0; $i<=10; $i++) {

    if ($counter%3===0) {
        echo 'something else';
    } else {
        echo 'normal';
    }

    echo '<br />';

    $counter++;
}