https://ja.wordpress.org/plugins/custom-post-type-ui/
問答無用でカスタムポストタイプUIをインストールします。

スポンサードリンク


WordPressは以下の順番でファイルを探しに行きます。
1. archive-{post_type}.php (ここではarchive-news.phpになります)
2. archive.php
3. index.php
ページャをつける関数は以下になります。
1 |
<?php the_posts_pagination() ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<div class="container"> <div class="row"> <div class="col-sm-12 col-md-9 news-list"><!--メインカラム--> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <div class="row news-list-item"> <div class="col-md-2"> <p class="news-date"><?php the_time("Y-m-d"); ?></p> </div><!-- .col end --> <div id="post-<?php the_ID(); ?>" <?php post_class("col-md-10"); ?>> <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2> <p><?php the_excerpt(); ?></p> </div> </div> <?php endwhile; ?> <?php wp_reset_postdata(); ?> <?php endif; ?> <!-- ページネーション --> <?php the_posts_pagination() ?> </div><!-- .col end --> <div class="col-sm-12 col-md-3 news_sidebar"> <h4>一年前までのNEWS</h4><!--サイドバー--> <ul> <?php $args = [ 'type' => 'monthly', 'limit' => '12',//12ヶ月分取得 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC', 'post_type' => 'news' ]; $out = wp_get_archives($args); $out = str_replace('</a>', '年</a>', $out); echo $out; ?> </ul> </div><!-- .col end --> </div><!-- .row --> </div><!-- .container end --> |
全体でこんな感じになります。
シングルページはsingle-news.phpになります。
カスタムポストタイプのカテゴリごとにアーカイブを表示する



newsカスタムポストタイプの一番下にある「利用するタクソノミー」にcheckを入れます。忘れがちです。


「階層をtrue」にすることでボックスが現れます。カテゴリーを選んで投稿しましょう。
https://example/タクソノミー/スラッグ/
でアーカイブを作成することが出来ます。
つまり
https://example/important/heavy/
で超重要なニュースのみのカテゴリーアーカイブを作成することができるのです。
おまけ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<div class="container"> <div class="row"> <div class="col-md-12 mt30 mb40"> <?php $url = $_SERVER['REQUEST_URI']; if (strstr($url, 'heavy') == true) { echo '<h2 class="text-center">' . '超重要ニュース</h2>'; } elseif (strstr($url, 'middle') == true) { echo '<h2 class="text-center">' . '程々のニュース</h2>'; } elseif (strstr($url, 'small') == true) { echo '<h2 class="text-center">' . '大したことないニュース</h2>'; } else { echo '<h2 class="text-center">' . 'ニュース</h2>'; } ?> </div> <!-- .col end --> </div><!-- .row --> </div><!-- .container end --> |
urlに応じてタイトルが変更するように分岐条件をつけました。本題とはズレてますけど。
サイドバーには以下のようにして、各カテゴリのリンクを作成しました。
1 2 3 4 5 6 7 8 |
<ul class="sidebar"> <?php $terms = get_terms('important'); foreach ($terms as $term) { echo '<li><a href="' . get_term_link($term->slug, 'important') . '">' . $term->name . '</a></li>'; //importantは独自に変えましょう。 } ?> </ul> |
これで自動的にカテゴリーが生成されます。
補足
カテゴリアーカイブが何故か逆順に表示されてしまう!!!という症状の方は「Simple Page Ordering」というプラグインをインストールしていませんか?その場合、プラグインの設定から「すべてを選択」を選んでみましょう。治ります!!!