php - 如何反转数组数据?

标签 php css arrays wordpress

我希望数组 1,3,5,7,9 的布局与数组 0,2,4,6,8,10 的布局相反 例如,第一行布局 - 左边是图片,右边是描述,第二行布局 - 右边是图片,左边是描述,连续不断。

这是我的function.php代码

<?php
            foreach ( $termchildren as $child ) {
                $term = get_term_by( 'id', $child, $taxonomy_name );
                $image = get_field('featured_image', $term);

                ?>
                <div class="row">
                    <div id="product-cat <?php echo $term->slug ?>">
                        <div class="two-col product-cat-image">
                            <img src="<?php echo $image ?>">
                        </div>
                        <div class="two-col product-cat-details">
                            <?php
                                echo '<h4>'. $term->name .'</h4>';
                                echo '<p>'. $term->description .'</p>';
                                echo '<a class="product-cat-button" href="' . get_term_link( $child, $taxonomy_name ) . '">See Products</a>';
                            ?>
                        </div>
                    </div>
                </div><?php
            } ?>

CSS 代码:

.row{
  display: flex;
  margin-left: -10px;
  margin-right: -10px;
  margin-bottom: 15px;
}
.row .col {
  flex: 1;
  padding-right: 10px;
  padding-left: 10px;
}

结果还是这样 result

我的期望是这样的: expectation

最佳答案

我们只需要更改 function.php 中的代码如下。

<?php
    $i=0; 
    foreach ( $termchildren as $child ) {
        $term = get_term_by( 'id', $child, $taxonomy_name );
        $image = get_field('featured_image', $term);
        if($i % 2){ ?>
            <div class="row">
                <div id="product-cat <?php echo $term->slug ?>">
                    <div class="two-col product-cat-image">
                        <img src="<?php echo $image ?>">
                    </div>
                    <div class="two-col product-cat-details">
                        <?php
                            echo '<h4>'. $term->name .'</h4>';
                            echo '<p>'. $term->description .'</p>';
                            echo '<a class="product-cat-button" href="' . get_term_link( $child, $taxonomy_name ) . '">See Products</a>';
                        ?>
                    </div>
                </div>
            </div>
        <?php } else {?>
            <div class="row">
                <div id="product-cat <?php echo $term->slug ?>">
                    <div class="two-col product-cat-details">
                        <?php
                            echo '<h4>'. $term->name .'</h4>';
                            echo '<p>'. $term->description .'</p>';
                            echo '<a class="product-cat-button" href="' . get_term_link( $child, $taxonomy_name ) . '">See Products</a>';
                        ?>
                    </div>
                    <div class="two-col product-cat-image">
                        <img src="<?php echo $image ?>">
                    </div>
                </div>
            </div>
        <?php }
       $i++;
    }
?>

希望现在你已经准备好了所有的东西,让我知道还需要什么帮助。

关于php - 如何反转数组数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56015335/

相关文章:

php - 从数据库中获取的字段长度的奇怪问题

html - 粘性标题和表格组合打破了流程

CSS 网格不会折叠单元格以响应 img 变换

html - Accordion 内元素的弹出窗口(Bootstrap)

arrays - 如何将 ArrayFormula 与数组一起使用(聚合后)?

java - 识别数组方法

php - MySql 和 ADODB5 - 最大连接数

php - 用于 PHP 的自动 Sprite 创建和 css mixin 脚本?

java - 无法在java中打印字符数组?可能编码?

php - 为什么 PHP-FPM 在写入标准输出时会添加警告前缀?