※社内専用

オリーブカート カテゴリ一覧全件表示

  • HOME
  • EPRESS-BS
  • オリーブカート カテゴリ一覧全件表示
オリーブカートのカテゴリ一覧ページで
全商品を表示させたいとの依頼。
もう少しいい方法があるかもなんですが
とりあえず対応した内容を書きます。

対応したページ

https://www.oneseed-oita.co.jp/product_category/sunshinecloud/

参照

・wp_queryのタクソノミーを使った記事の取得方法
https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/WP_Query#.E3.82.BF.E3.82.AF.E3.82.BD.E3.83.8E.E3.83.9F.E3.83.BC.E3.81.AE.E3.83.91.E3.83.A9.E3.83.A1.E3.83.BC.E3.82.BF ・term_idの取得
https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_the_terms

内容

以下はループの中の記述です。
$taxonomy_id = get_the_terms( $post->ID, 'product_category' );
$args = array(
    'post_type' => 'post',
    'tax_query' => array(
                        'taxonomy' => 'product_category',
                        'field'    => 'term_id',
                        'terms'    => array( $taxonomy_id[0]->term_taxonomy_id )
                        ),
    ),
);
$query = new WP_Query( $args );

タームとかよくわかんない。(解説)

term_id

商品カテゴリを作る。 wp_termsテーブルに
作ったカテゴリが表示されている。 term_idが記述されている。 上記のwp_queryの引数で書かれている。
'field'    => 'term_id'
fieldの値をterm_idに指定したら
termsの値にterm_idを入れる。

term_idの取得

上記の参照にある、get_the_termsで取得できる。

全体

@layout('view.master')

@section('content')
  @wpposts
    {{the_content()}}
  @wpempty
  @wpend
  <div class="row row_inline item_list">
<?php
    $paged = get_query_var('paged');
    $item_category = get_the_terms( $post->ID, 'product_category' );
    $args = array(
      'posts_per_page' => -1,
      'tax_query' => array(
                      array(
                        'taxonomy' => 'product_category',
                        'field'    => 'term_id',
                        'terms'    => array( $item_category[0]->term_taxonomy_id ),
                      ),
                    )
    );
    $the_query = new WP_Query( $args );

    if($the_query->have_posts()) {
      while ( $the_query->have_posts() ) : $the_query->the_post();
      $html = '';
        $get_meta = $wpdb->get_results(
          $wpdb->prepare( "SELECT * FROM ".$wpdb->prefix . 'cart_meta' . " WHERE post_id = %d", $post->ID )
        );
        $prices = $get_meta[0]->item_option_price;
        $priceList = explode( ':', $prices );
        $item = get_field('item');
        $tax = get_option('consumption_tax');

        $html .='<div class="col span_4">
          <figure>
            <a href="'.get_permalink($post->ID).'" class="item_list_link">
              <img src="'.$item[url].'" alt="'.get_the_title().'">
            </a>
            <figcaption>
              <span class="item_all_title">'.$get_meta[0]->item_title.'</span><br>
              <span class="item_all_price"><span>&yen;&nbsp;'.number_format($priceList[0]) .'</span></span>';
        if($tax){
          $html .='(税抜)';
        }else{
          $html .='(税込)';
        }
        $html .='</figcaption></figure></div>';

        echo $html;

      endwhile;
      wp_reset_postdata();
    }

?>
  </div>

  <?php
    // ページネーション表示前に$GLOBALS['wp_query']->max_num_pagesに値をセット
    // $GLOBALS['wp_query']->max_num_pages = 1000;
    // the_posts_pagination();
    // wp_reset_postdata();
  ?>
@endsection