Почему wc_get_template_part () не работает внутри wc_get_products ()? - PullRequest
0 голосов
/ 01 августа 2020

Не удается найти полный пример l oop в сети с wc_get_template_part() и wc_get_products(), поэтому ищите помощь здесь:

            global $woocommerce;
            global $product;
            $args = array(
                'limit' => 15,
                'category' => array('printers', 'laptop')
            );
            $query_cats = wc_get_products($args);

            foreach ($query_cats as $query_cat) {
               
                echo $query_cat->get_id();
                echo $query_cat->get_title();
                // echo "<pre>";
                // var_dump($query_cat);
                wc_get_template_part('content', 'product');
            }
            
            ?>

Отображаются заголовки и идентификаторы, также var_dump, bu wc_get_template_part - нет. У меня add_theme_support('woocommerce'); и also body_class();

1 Ответ

1 голос
/ 04 августа 2020

WooCommerce content-product. php шаблон работает только с стандартным l oop (с экземпляром Wp_Query) . Может помочь следующее решение:

$args = array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'tax_query'     => [
      array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => ['printers', 'laptop'],
      )
    ],
);

$product = new WP_Query( $args );

while ( $product->have_posts() ) {
    $product->the_post();

    wc_get_template_part( 'content', 'product' );

}
wp_reset_postdata();

Спасибо

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...