0

I have the following code and i cant print the term. I use .&brand->name. but anything happens. If i type the exactly words it works but i need it work and o other brand taxonomies too.

Any help?

     <?php
    // get products
    $args = array(
  'post_type'      => 'product',
  'posts_per_page' => -1,
  'tax_query'      => array(
    array(
      'taxonomy' => 'pwb-brand', //brands are terms of 'pwb-brand' taxonomy
      'field'    => 'name', //search by term name
      'terms'    => array ('how to print????' ) //brand names here
    )
  )
);
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
      while ( $loop->have_posts() ) : $loop->the_post();
        wc_get_template_part( 'content', 'product' );
      endwhile;
    } else {
      echo __( 'No results.' );
    }
    wp_reset_postdata();
  ?>

1 Answer 1

0

If you want to output/print the term name look at this within the loop: https://developer.wordpress.org/reference/functions/get_the_terms/

$terms = get_the_terms( get_the_ID(), 'pwb-brand' );

You will need to output within the loop similar to this:

<?php
    $loop = new WP_Query( $args );
    if ( $loop->have_posts() ) {
      while ( $loop->have_posts() ) : $loop->the_post();
        $terms = get_the_terms( get_the_ID(), 'pwb-brand' );

        if ( $terms && ! is_wp_error( $terms ) ) : 

        $brands = array();

        foreach ( $terms as $term ) {
            $brands[] = $term->name;
        }

        $on_brands = join( ", ", $brands );
        ?>

        <p class="beers draught">
            <?php printf( esc_html__( 'Brands: <span>%s</span>', 'textdomain' ), esc_html( $on_brands ) ); ?>
        </p>
        <?php endif; 
        wc_get_template_part( 'content', 'product' );
      endwhile;
    } else {
      echo __( 'No results.' );
    }
    wp_reset_postdata();
  ?>

You could put the additional code within the content-product.php template file.

Your question says you want to print the terms, but your initial query is trying to filter the results. If so:

$args = array(
  'post_type'      => 'product',
  'posts_per_page' => -1,
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.