G
G
Genri_Rus2020-01-28 21:21:07
WordPress
Genri_Rus, 2020-01-28 21:21:07

Is it possible to include content-product in the taxonomy-product_cat file?

Here is an example:

<?php
    $args = array(
      'post_type' => 'product',
      'tax_query' => array(
        array(
          'taxonomy' => 'product_cat',
          'field'    => 'name',
          'terms'    => 'featured',
        ),
      ),
      'posts_per_page' => -1,
    );
    
    global $wp_query;
    
    $wp_query = new WP_Query( $args );

    if ( $wp_query->have_posts() ) :
  ?>

    <?php while( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
      <?php wc_get_template_part( 'content', 'content-product' ); ?>
    <?php endwhile; ?>
      
  <?php else : ?>
    <div>Товаров не найдено</div>
  <?php endif; ?>


Is it possible to somehow display products of a certain category in this way?
It just doesn't work for me in this way
Or is it necessary to create a file content-product-cat.php ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2020-01-28
@Genri_Rus

It is possible, but why not, but 2 clarifications
1. 'field' => 'name', will search for the title of the category and not the slug,
2. wc_get_template_part( 'content', 'content-product' ); - will look for the template content-content-product.php and not content-product.php
well, the nuances for ul
should look something like this

<?php
  /**
   * Template Name: Бла бла бла
   */

  get_header();
  $args = [
    'post_type'      => 'product',
    'tax_query'      => [
      [
        'taxonomy' => 'product_cat',
        'field'    => 'name',
        'terms'    => 'Tem Name',
      ],
    ],
    'posts_per_page' => - 1,
  ];

  global $wp_query;

  $wp_query = new WP_Query( $args );

  if ( $wp_query->have_posts() ) :

    woocommerce_product_loop_start();
    ?>

    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post();
    ?>
    <?php wc_get_template_part( 'content', 'product' ); ?>
  <?php endwhile;
    woocommerce_product_loop_end();
    ?>

  <?php else : ?>
        <div>Товаров не найдено</div>
  <?php endif;
  get_footer();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question