递增自定义遍历器中的类

时间:2012-02-03 作者:gillespieza

我已经编写了一个与我一起使用的自定义助行器wp_nav_menu 但我需要遍历4个自定义类,我不知道如何做到这一点。基本上,每个菜单项<li> 需要给一类color1, color2, color3color4, 循环返回到color1, 泡沫,冲洗,重复,按顺序。

通常我会用$i 和a$i++, 但是在沃克的课堂上我该怎么做呢?

编辑:我的Walker课程代码:

class Salamander_Advent_Walker extends Walker_page {
function start_el(&$output, $item, $depth, $args) {
    if ( $depth ) {
        $indent = str_repeat("\\t", $depth);
    } else {
        $indent = \'\';
    }

    $advent_thumbnail = get_post_meta($item->ID, \'advent-thumb\', true);
    $advent_slug = get_post_meta($item->ID, \'advent-slug\', true);
    $advent_oneliner = get_post_meta($item->ID, \'advent-oneliner\', true);
    //$description = get_post_meta($item->ID, \'advent-thumb\', true);

    $output .= $indent . \'
    <li class="active color4">
        <a href="#day\'. $advent_slug .\'">
            <span class="day">
                <strong>\'. $advent_slug .\'</strong>
                <span>&nbsp;</span>
            </span>
            <span class="content">
                <small>\'. $advent_slug .\'</small>
                <img src=\'. $advent_thumbnail .\' width="126" height="91" alt="advent" />
                <strong>\'. $advent_oneliner .\'</strong>
            </span>
        </a>\';                      
}
}

3 个回复
最合适的回答,由SO网友:sirlark 整理而成

使用类变量保持要显示的颜色的“索引”。每次使用它时递增,并检查它是否超过,在这种情况下,将其设置回1。

class Salamander_Advent_Walker extends Walker_page {
    private $color_idx = 1;
    function start_el(&$output, $item, $depth, $args) {
        if ( $depth ) {
            $indent = str_repeat("\\t", $depth);
        } else {
            $indent = \'\';
        }

        $advent_thumbnail = get_post_meta($item->ID, \'advent-thumb\', true);
        $advent_slug = get_post_meta($item->ID, \'advent-slug\', true);
        $advent_oneliner = get_post_meta($item->ID, \'advent-oneliner\', true);
        //$description = get_post_meta($item->ID, \'advent-thumb\', true);

        $output .= $indent . \'
        <li class="active color\'.$this->color_idx.\'">
            <a href="#day\'. $advent_slug .\'">
                <span class="day">
                    <strong>\'. $advent_slug .\'</strong>
                    <span>&nbsp;</span>
                </span>
                <span class="content">
                    <small>\'. $advent_slug .\'</small>
                    <img src=\'. $advent_thumbnail .\' width="126" height="91" alt="advent" />
                    <strong>\'. $advent_oneliner .\'</strong>
                </span>
            </a>\';              
        $this->color_idx++;
        if ($this->color_idx > 4) {
            $this->color_idx = 1
        }
    }

}

SO网友:fuxia

创建单独的函数:

function alternate()
{
    static $i = 0;
    $args = func_get_args();

    return $args[ $i++ % func_num_args() ];
}
在您的助行器中,请这样使用:

$class = \'color\' . alternate( 1, 2, 3, 4 );
// somewhere in your output string:
$output .= \'<li class="active \' . $class . \'">\';

SO网友:Drew Gourley

可以通过将递增的数字设置为全局变量来实现这一点。

function start_el(&$output, $item, $depth, $args) {
    //globalize the variable
    global $ac_color_num;
    //check if it is set, or if it is 4 (to reset to 1)
    if ( !isset($ac_color_num) || $ac_color_num == 4 ) { $ac_color_num = 1; }
    $ac_color_num++
    if ( $depth ) {
        $indent = str_repeat("\\t", $depth);
    } else {
        $indent = \'\';
    }

    $advent_thumbnail = get_post_meta($item->ID, \'advent-thumb\', true);
    $advent_slug = get_post_meta($item->ID, \'advent-slug\', true);
    $advent_oneliner = get_post_meta($item->ID, \'advent-oneliner\', true);
    //$description = get_post_meta($item->ID, \'advent-thumb\', true);

    //use $ac_color_num in the output.
    $output .= $indent . \'
    <li class="active color\' . $ac_color_num . \'">
        <a href="#day\'. $advent_slug .\'">
            <span class="day">
                <strong>\'. $advent_slug .\'</strong>
                <span>&nbsp;</span>
            </span>
            <span class="content">
                <small>\'. $advent_slug .\'</small>
                <img src=\'. $advent_thumbnail .\' width="126" height="91" alt="advent" />
                <strong>\'. $advent_oneliner .\'</strong>
            </span>
        </a>\';                      
}

结束