【问题标题】:While loop: Output something different on every second resultWhile循环:每秒输出不同的结果
【发布时间】:2010-05-11 01:44:13
【问题描述】:

我正在为 WordPress 运行一个名为 Category Posts Widget 的插件:http://wordpress.org/extend/plugins/category-posts/

它使用一个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。

谢谢,

韦德

【问题讨论】:

    标签: php wordpress plugins while-loop


    【解决方案1】:

    以下内容未经测试,但它说明了基本原理。只需在循环的每个实例上切换一个布尔值。甚至帖子也会额外包含 cat-post-item-even 类,这样您就不必过多地修改样式表。

    很高兴发现 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";
    

    【讨论】:

      【解决方案2】:
      // ....
      <? $type = ($type + 1) % 2; ?>
      <li class="cat-post-item<?=$type ?>">
      

      【讨论】:

        【解决方案3】:
        $counter=1;
        
        for ($i=0; $i<=10; $i++) {
        
            if ($counter%3===0) {
                echo 'something else';
            } else {
                echo 'normal';
            }
        
            echo '<br />';
        
            $counter++;
        }
        

        【讨论】:

        • 你说得对,我看错了问题;认为他 ment 001001.. 反对 010101.. 其他方式:if ($counter &amp; 1) { }
        猜你喜欢
        • 2011-02-23
        • 2012-11-15
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2018-04-02
        • 1970-01-01
        • 2017-05-19
        • 1970-01-01
        相关资源
        最近更新 更多