R
R
Roman Govorov2020-12-23 12:32:05
WordPress
Roman Govorov, 2020-12-23 12:32:05

How to display products in default form using WP_Query?

That is not a new pattern to write like:

<?php
while ( $loop->have_posts() ): $loop->the_post(); ?>
    <div <?php post_class("inloop-product"); ?>>
        <div class="row">
            <div class="col-sm-4">
                <?php the_post_thumbnail("thumbnail-215x300"); ?>
            </div>
            <div class="col-sm-8">
                <h4>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h4>
                <?php the_content(); ?>
                <p class="price">
                    <?php _e("Price:","examp"); ?>
                    <?php woocommerce_template_loop_price(); ?>
                </p>
                <?php woocommerce_template_loop_add_to_cart(); ?>
            </div>
        </div>
    </div>
<?php endwhile;?>


And immediately display the product in its default shell:
<?php
while ( $loop->have_posts() ): $loop->the_post();
    $loop->the_product();
endwhile;
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Govorov, 2020-12-23
@RGameShow

I solved the issue with crutches, but maybe someone knows a better solution?

<section class="">
    <div class="container">
        <?php
        $loop = new WP_Query( array(
            'post_type' => 'product',
            'posts_per_page' => 2+1,
            'orderby' => 'menu_order',
            'order' => 'ASC',
        ));
        ?>
        <div class="woocommerce">
            <ul class="products products_archive_grid">
                <?
                while ( $loop->have_posts() ): $loop->the_post();
                    $id = get_the_ID();
                    $short = do_shortcode('[product id="'.$id.'"]');
                    $short = str_replace(
                        "<div class=\"woocommerce \"><ul class=\"products products_archive_grid\">",
                        "",
                        $short);
                    $short = substr($short,0,-11);
                    echo $short;
                endwhile;
                ?>
            </ul>
        </div>
    </div>
</section>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question