サポート » 使い方全般 » カテゴリー別記事 一覧ページを作成したい

  • 解決済 pandora001

    (@pandora001)


    お世話になります。

    現在、カテゴリー別の記事一覧ページを作成しようとしているのですが、上手くいきません。
    ご教授いただけると幸いです。

    制作したいページは下記のような内容です。

    ・カテゴリー別の記事一覧ページを作成
    ・カテゴリーは、カスタム投稿内でカテゴリー別に分かれている(親、子の2階層)

    現在下記の様にしてカテゴリー分けしようとしているのですが、上手くいきません。

    
    <main class="main
     bg_beige">
      <div class="index-newproperty__category">
        カテゴリ
        <a href="#" class="index-newproperty__categorybtn btn__rent">賃貸</a>
        <a href="#" class="index-newproperty__categorybtn btn__sale">売買</a>
      </div>
      <div class="index-newproperty__box">
      <?php
      $paged = (int) get_query_var('paged');
      $args = array(
        'posts_per_page' => 6,
        'paged' => $paged,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'property',
        'terms'=> 'sale',
        'post_status' => 'publish'
      );
    
      $the_query = new WP_Query($args);
      if ( $the_query->have_posts() ) :
        while ( $the_query->have_posts() ) : $the_query->the_post();
      ?>
      <?php
        $cat = get_the_category();
        $cat_slug  = $cat[0]->category_nicename;
      ?>
    
                  <article class="index-newproperty measures bg_white <?php echo $cat_slug ?>">
                    <a href="<?php the_permalink(); ?>">
                      <img class="news__article__img" src="<?php the_post_thumbnail_url(); ?>" height="250px">
                      <div class="index-property__content-text bg_white">
                        <h4 class=""><?php the_title(); ?></a></h4>
                        <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_time('Y.m.d'); ?></time>
                        <p><?php the_excerpt('Read more'); ?></p>
                        <p class="index-property__content-more">MORE</p>
                      </div>
                    </a>
                  </article>
    
      <?php endwhile; endif; ?>
    
      <div class="pagination">
      <?php
      if ($the_query->max_num_pages > 1) {
        echo paginate_links(array(
          'base' => get_pagenum_link(1) . '%_%',
          'category_name' => 'rent',
          'format' => 'page/%#%/',
          'current' => max(1, $paged),
          'total' => $the_query->max_num_pages,
          'prev_text' => '',
          'next_text' => ''
        ));
      }
      ?>
      </div>
    

    現在の表示は、カスタム投稿内の記事一覧が出てしまい、希望する以外のカテゴリーの記事も出てしまっている状態です。
    一覧で表示したいカテゴリーのスラッグは、”sale”です。

    説明不足な部分もあると思いますが、お助けいただけると幸いです。
    よろしくお願いします。

5件の返信を表示中 - 1 - 5件目 (全5件中)
  • 'terms'=> 'sale',

    'category_name'=> 'sale',

    では、どうでしょうか?

    トピック投稿者 pandora001

    (@pandora001)

    ishitaka (@ishitaka)様

    ご連絡ありがとうございます。
    試してみたのですが、上手くいきませんでした。

    投稿一覧に記事が何も表示されなくなりました。

    カスタム投稿の設定がおかしいのでしょうか⁇
    カスタム投稿の設定は下記の様になっております。

    functions.php

    
    function create_post_type_detail_property() {
      $Supports = [
        'title',
        'editor',
        'thumbnail',
      ];
      register_post_type( 'property',
        array(
          'label' => '物件',
          'labels' => array(
          'all_items' => '物件一覧'
          ),
          'public' => true,
          'has_archive' => true,
          'menu_position' => 5,
          'supports' => $Supports
        )
      );
        //カテゴリタイプの設定
      register_taxonomy(
        'property', //カテゴリー名(任意)
        'property', //カスタム投稿名
        array(
          'hierarchical' => true, //カテゴリータイプの指定
          'update_count_callback' => '_update_post_term_count',
          //ダッシュボードに表示させる名前
          'label' => '物件',
          'public' => true,
          'show_ui' => true
        )
      );
    }
    

    なになら何までお聞きしてしまい申し訳ありません。

    トピック投稿者 pandora001

    (@pandora001)

    子カテゴリーは管理画面にて設定しました。

    お手間おかけしますが、よろしくお願いします。

    'terms'=> 'sale',

    'tax_query' => array(
    	array(
    		'taxonomy' => 'property',
    		'field' => 'slug',
    		'terms' => 'sale',
    	),
    ),

    では、どうでしょうか?

    トピック投稿者 pandora001

    (@pandora001)

    ishitaka (@ishitaka)様

    ありがとうございます。
    試しましたら、希望していた状態になりました。
    本当にありがとうございます。

5件の返信を表示中 - 1 - 5件目 (全5件中)
  • トピック「カテゴリー別記事 一覧ページを作成したい」には新たに返信することはできません。