有关打印术语的WP_QUERY的帮助

时间:2018-01-16 作者:sot

我有以下代码,我无法打印该术语。我使用.&brand->name. 但一切都会发生。如果我键入的是正确的单词,那么它就可以工作,但我需要它工作,并且还需要其他品牌分类法。

有什么帮助吗?

     <?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 个回复
SO网友:tricky x

如果要输出/打印术语名称,请在循环中查看:https://developer.wordpress.org/reference/functions/get_the_terms/

$terms = get_the_terms( get_the_ID(), \'pwb-brand\' );
您需要在循环中输出类似的内容:

<?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();
  ?>
您可以在内容产品中添加其他代码。php模板文件。

您的问题表示要打印术语,但您的初始查询正在尝试筛选结果。如果是:

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

结束