在迭代wordpress中包裹元素

时间:2014-09-09 15:09:49

标签: wordpress foreach counter

我写了第二段代码

            <?php
                $subcategories = get_categories('&child_of=1&hide_empty=0'); // List subcategories of category '4' (even the ones with no posts in them)
                foreach ($subcategories as $subcategory) {
                    $counter = 0;
                    $counter++; 
                    ?>

                    <?php if ($subcategory->parent == 1) { 
                    if ($counter%2) {
                        echo '<div class="rowcat">';
                    }
                        ?>

                    <!-- begin catblock  -->
                    <div class="catblock">
                        <?php echo '<img src="'. z_taxonomy_image_url($subcategory->term_id) .'" />'; ?>
                        <?php echo sprintf('<a href="%s" class="catblock__title">%s</a>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?>
                    </div>
                    <!-- end catblock -->

                    <?php 
                    if ($counter%2) {
                        echo '</div>';
                    }
                    ?>

                    <?php } ?>
                <?php } ?>

现在每个<div class="catblock"></div><div class="rowcat">,但我想让<div class="rowcat">

包裹的每两个<div class="rowcat">

所以我做错了什么,这是什么?

2 个答案:

答案 0 :(得分:0)

$counter++移至循环的结尾:

        <?php
            $subcategories = get_categories('&child_of=1&hide_empty=0'); // List subcategories of category '4' (even the ones with no posts in them)
            foreach ($subcategories as $subcategory) {
                $counter = 0;                    
                ?>

                <?php if ($subcategory->parent == 1) { 
                if ($counter%2) {
                    echo '<div class="rowcat">';
                }
                    ?>

                <!-- begin catblock  -->
                <div class="catblock">
                    <?php echo '<img src="'. z_taxonomy_image_url($subcategory->term_id) .'" />'; ?>
                    <?php echo sprintf('<a href="%s" class="catblock__title">%s</a>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?>
                </div>
                <!-- end catblock -->

                <?php 
                if ($counter%2) {
                    echo '</div>';
                }
            }
            $counter++;  
        } ?>

答案 1 :(得分:0)

我找到了正确的解决方案

<?php
            $subcategories = get_categories('&child_of=1&hide_empty=0'); // List subcategories of category '4' (even the ones with no posts in them)
            $counter = 0; 
            foreach ($subcategories as $subcategory) { ?>

                <?php if ($subcategory->parent == 1) { 
                if ($counter%2 == 0) {
                    echo $counter > 0 ? "</div>" : "";
                    echo '<div class="rowcat">';
                }
                    ?>

                <!-- begin catblock  -->
                <div class="catblock">
                    <?php echo '<img src="'. z_taxonomy_image_url($subcategory->term_id) .'" />'; ?>
                    <?php echo sprintf('<a href="%s" class="catblock__title">%s</a>', get_category_link($subcategory->term_id), apply_filters('get_term', $subcategory->name));?>
                </div>
                <!-- end catblock -->

                <?php 
            }
            $counter++;  
        } ?>