如何将WP_QUERY中的<ul>添加到显示的每3个帖子中?

时间:2017-05-07 作者:Rhian Molinari

我想在一个<ul>

HTML外观示例:

<ul>
    <li>product looping 1</li>
    <li>product looping 2</li>
    <li>product looping 3</li>
</ul>
<ul>
    <li>product looping 4</li>
    <li>product looping 5</li>
    <li>product looping 6</li>
</ul>
WP\\U查询代码:

<?php
        $args = array(
            \'post_type\' => \'product\',
            \'posts_per_page\' => 8,
            \'orderby\' => \'date\',
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();

                //wc_get_template_part( \'content\', \'product\' );

            ?>
            <li class="col-md-3">
                <div class="gridproduto">
                    <a href="<?php the_permalink(); ?>">
                        <?php global $post, $product; ?>
                        <?php if ( $product->is_on_sale() ) : ?>
                            <?php echo apply_filters( \'woocommerce_sale_flash\', \'<span class="onsale">\' . __( \'Sale!\', \'woocommerce\' ) . \'</span>\', $post, $product ); ?>
                        <?php endif; ?>

                        <span class="thumbnail-product">
                            <?php the_post_thumbnail( \'medium\' ); ?>
                        </span>
                    </a>
                        <div class="main-infos">
                            <a href="<?php the_permalink(); ?>">
                                <h5><?php the_title(); ?></h5>
                                <div><?php echo $product->get_price_html(); ?></div>
                                <span class="preco_boleto">
                                    <?php 
                                    $boleto = $product->get_price();
                                    $desconto = 10; 
                                    $division = $boleto - ( $boleto * ($desconto / 100) );
                                    echo "R$ " . number_format( round($division, 2), 2, \',\', \'.\' );
                                    ?>
                                </span>
                                <span class="parcela">no boleto ou em até <br> 3x de <?php echo number_format( round( $product->get_price() / 3, 2), 2, \',\', \'.\' ); ?> sem juros.</span>
                            </a>
                            <a href="#" class="btn-orange">Comprar</a>
                        </div>

                </div>
            </li>

            <?php endwhile;
        } else {
            echo __( \'No products found\' );
        }
        wp_reset_postdata();
    ?>

2 个回复
SO网友:rudtek

This would work:

<?php
        $args = array(
            \'post_type\' => \'product\',
            \'posts_per_page\' => 8,
            \'orderby\' => \'date\',
            );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            $i=1;
            echo \'<ul>\';
            while ( $loop->have_posts() ) : $loop->the_post();

                //wc_get_template_part( \'content\', \'product\' );

            ?>
            <li class="col-md-3">
                <div class="gridproduto">
                    <a href="<?php the_permalink(); ?>">
                        <?php global $post, $product; ?>
                        <?php if ( $product->is_on_sale() ) : ?>
                            <?php echo apply_filters( \'woocommerce_sale_flash\', \'<span class="onsale">\' . __( \'Sale!\', \'woocommerce\' ) . \'</span>\', $post, $product ); ?>
                        <?php endif; ?>

                        <span class="thumbnail-product">
                            <?php the_post_thumbnail( \'medium\' ); ?>
                        </span>
                    </a>
                        <div class="main-infos">
                            <a href="<?php the_permalink(); ?>">
                                <h5><?php the_title(); ?></h5>
                                <div><?php echo $product->get_price_html(); ?></div>
                                <span class="preco_boleto">
                                    <?php 
                                    $boleto = $product->get_price();
                                    $desconto = 10; 
                                    $division = $boleto - ( $boleto * ($desconto / 100) );
                                    echo "R$ " . number_format( round($division, 2), 2, \',\', \'.\' );
                                    ?>
                                </span>
                                <span class="parcela">no boleto ou em até <br> 3x de <?php echo number_format( round( $product->get_price() / 3, 2), 2, \',\', \'.\' ); ?> sem juros.</span>
                            </a>
                            <a href="#" class="btn-orange">Comprar</a>
                        </div>

                </div>
            </li>

            <?php
            if ($i % 3 == 1){
                echo \'</ul><ul>\';
            }
            $i++;
            endwhile;
            echo \'</ul>\';
        } else {
            echo __( \'No products found\' );
        }
        wp_reset_postdata();
    ?>
SO网友:Aishan

请使用以下代码:

    $break_after = 3;
    $counter = 0;
    $args = array(
                \'post_type\' => \'product\',
                \'posts_per_page\' => 8,
                \'orderby\' => \'date\',
                );
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
        while ( $loop->have_posts() ) : $loop->the_post(); 
            if ($counter % $break_after == 0) {
                echo \'<ul>\';
            } ?>

            <li class="col-md-3">
                <div class="gridproduto">
                    <a href="<?php the_permalink(); ?>">
                        <?php global $post, $product; ?>
                        <?php if ( $product->is_on_sale() ) : ?>
                            <?php echo apply_filters( \'woocommerce_sale_flash\', \'<span class="onsale">\' . __( \'Sale!\', \'woocommerce\' ) . \'</span>\', $post, $product ); ?>
                        <?php endif; ?>

                        <span class="thumbnail-product">
                            <?php the_post_thumbnail( \'medium\' ); ?>
                        </span>
                    </a>
                        <div class="main-infos">
                            <a href="<?php the_permalink(); ?>">
                                <h5><?php the_title(); ?></h5>
                                <div><?php echo $product->get_price_html(); ?></div>
                                <span class="preco_boleto">
                                    <?php 
                                    $boleto = $product->get_price();
                                    $desconto = 10; 
                                    $division = $boleto - ( $boleto * ($desconto / 100) );
                                    echo "R$ " . number_format( round($division, 2), 2, \',\', \'.\' );
                                    ?>
                                </span>
                                <span class="parcela">no boleto ou em até <br> 3x de <?php echo number_format( round( $product->get_price() / 3, 2), 2, \',\', \'.\' ); ?> sem juros.</span>
                            </a>
                            <a href="#" class="btn-orange">Comprar</a>
                        </div>

                </div>
            </li>

        <?php 
            if ($counter % $break_after == ($break_after-1)) {
                echo \'</ul>\';
            }
            ++$counter;
        endwhile;
    } else {
        echo __( \'No products found\' );
    }
    wp_reset_postdata();
希望有帮助!!

结束

相关推荐

how do i make this loop work

抱歉,我是一个PHp初学者,我不知道为什么这个循环对我的标签不起作用。php页面欢迎任何帮助。<?php get_header(); ?> <div class=\"posts\"><!-- BLOG --> <!-- Shapes on sides --> <div class=\"shapes_left\"> </div> <div class=\"shape