代码之家  ›  专栏  ›  技术社区  ›  Kenshinh

在两个容器中并排列出WooCommerce产品

  •  0
  • Kenshinh  · 技术社区  · 6 年前

    我编写了这个PHP代码来按特定类别列出WooCommerce产品。我的页面中有两个DIV容器,现在我想为每个容器逐个获取这些产品。代码将所有产品按应该的方式列出到container1中。但我一直在修改代码,以便让产品为每个容器并排列出。如果我的问题不清楚,这就是我简单的意思。

    这个代码的作用是,

    container1        container2
    
    product1          empty
    product2
    product3
    product4
    

    我要做的是,

    container1        container2
    
    product1          product2
    product3          product4
    

    代码是

    <?php
    
    $args = array( 'category' => array( 'shirts' ), );
    
    $productlist = wc_get_products( $args );
    
    // List products for container one
    
    echo '<div class="container1>'
    
    echo '<ul class="product-list-wrap">';
    
    // loop through the product list
    foreach ($productlist as $product) {
    
    $productid = $product->get_id(); // get individual product id
    $custom_field = get_field( 'custom_field', $productid ); // get the custom field
    
    echo '<a href="'.$product->get_permalink().'">
    <li class="product-name">'.$product->get_title().'<span class="custom">'.$custom_field.'</span>'.'</li>
    </a>';
    }
    
    echo '</ul>';
    
    echo '</div>';
    
    // List products for container two
    
    echo '<div class="container2>'
    
    echo '<ul class="product-list-wrap">';
    
    content
    
    echo '</ul>';
    
    echo '</div>';
    
    ?>
    

    HTML输出

    <div class="container1">
    
        <ul class="product-list-wrap">
    
            <a href="http://localhost/project1/product/product-1/">
            <li class="product-name">Product 1<span class="custom">custom output</span></li>
            </a>
    
            <a href="http://localhost/project1/product/product-2/">
            <li class="product-name">Product 1<span class="custom">custom output</span></li>
            </a>
    
            <a href="http://localhost/project1/product/product-3/">
            <li class="product-name">Product 1<span class="custom">custom output</span></li>
            </a>
    
            <a href="http://localhost/project1/product/product-4/">
            <li class="product-name">Product 1<span class="custom">custom output</span></li>
            </a>
    
        </ul>                            
    
    </div>
    
    <div class="container2">
    
        <ul class="product-list-wrap">
    
        content
    
        </ul>
    
    </div>
    
    2 回复  |  直到 6 年前
        1
  •  1
  •   Dominik Liss    6 年前

    $productList1 = array();
    $productList2 = array();
    
    foreach( $productlist as $key=>$product ) {
        if( $key % 2 == 0 ) {
            // even key (0, 2, 4, ...)
            array_push( $productList1, $product );
        }
        else {
            // odd key (1, 3, 5, ...)
            array_push( $productList2, $product );
        }
    }
    

        2
  •  1
  •   Mike Aron    6 年前
    <?php
    // Put all your products in 1 array
    $container_data = array();
    
    foreach ($productlist as $product) {
    
    $productid = $product->get_id(); // get individual product id
    $custom_field = get_field( 'custom_field', $productid ); // get the custom field
    
    // fill the array
    $container_data[] = '<a href="'.$product->get_permalink().'">
    <li class="product-name">'.$product->get_title().'<span class="custom">'.$custom_field.'</span>'.'</li>
    </a>';
    
    }
    
    // Split your array
    list($container_1, $container_2) = array_chunk($container_data, ceil(count($container_data) / 2));
    
    // You have now 2 arrays
    // $container_1
    // $container_2
    
    // Now loop again
    
    echo '<div class="container1>';
    
    echo '<ul class="product-list-wrap">';
    
    foreach ($container_1 as $product) {
    echo $product;
    }
    
    echo '</ul>';
    
    echo '</div>';
    
    // Now loop again
    
    echo '<div class="container2>';
    
    echo '<ul class="product-list-wrap">';
    
    foreach ($container_2 as $product) {
    echo $product;
    }
    
    echo '</ul>';
    
    echo '</div>';