如果li标签在ul中显示4次​​,则启动新的ul

时间:2018-01-17 19:55:13

标签: php wordpress

如果li标签显示4次,我需要启动新的<ul class="rd-navbar-megamenu">课程并关闭打开的<ul>课程。我正在使用它在菜单中添加新行。 我用下面的代码来显示来自wordpress的菜单。它运作良好唯一的事情就是我被困在显示li元素

<?php
    $count = 0;
    $a = 0;
    $submenu = false;
    foreach( $menuitems as $item ):
        $link = $item->url;
        $title = $item->title;
        // item does not have a parent so menu_item_parent equals 0 (false)
        if ( !$item->menu_item_parent ):
        // save this id for later comparison with sub-menu items
        $parent_id = $item->ID;

    ?>

    <li>
        <a href="<?php echo $link; ?>" class="title"> <?php echo $title; ?> </a>
        <?php endif;


        if ( $parent_id == $item->menu_item_parent ):   ?>

        <?php if ( !$submenu ): $submenu = true;

        ?>

        <ul class="rd-navbar-megamenu"> // this class should start if there are 4 items (li)
            <?php endif;  ?>
            <li>
                <ul class="rd-megamenu-list">
                    <li><a href="<?php echo $link; ?>" class="title"><?php echo $item->title; ?></a></li>
                </ul>
            </li>
          <?php

            if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id && $submenu ):

                if($a % 4 == 0) {
                    echo '</ul><ul class="rd-navbar-megamenu">';
                }
            ?>
        </ul>

        <?php $submenu = false; endif;

         endif;

        if ( $menuitems[ $count + 1 ]->menu_item_parent != $parent_id ):

        ?>

   </li>
    <?php $submenu = false; endif;

$count++;

endforeach;
?>

1 个答案:

答案 0 :(得分:0)

这就是你想要的:

<?php
    $count = 0;
    foreach($arr as $item){
        echo '<li></li>';
        if(!($count%4))
             echo '</ul><ul class="">';
        $count++;
    } ?>

在你的情况下它应该是这样的:

$a = 0;
$submenu = false;
foreach( $menuitems as $item ):
    if(!($a%4)) {
        echo '</ul><ul class="rd-navbar-megamenu">';
    }
    $a++; // I think you forget this part
endforeach;