wordpressのカスタム投稿でお知らせページを作成しています。
一覧ページから記事のカテゴリーをクリックすると、カテゴリー別の一覧ページを表示させるようにしたいのですが、
どのようなやり方を試しても404 Not Foundになり苦戦しております。
taxonomy.phpのやり方が良いのかなと試したのですが、こちらも上手くいかず…。
もしかしたら一覧のページとカスタム投稿の設定に何か不備があるのかも?と調べたのですが、
力不足のため見つかりませんでした。
下記、お知らせの一覧ページ(archive-news.php)と、カスタム投稿用のfunction.phpのコードになります。
■archive-news.php
php
1<?php 2$sdu = get_stylesheet_directory_uri(); 3$gbi = get_bloginfo('url'); 4 5get_header(); 6?> 7 8<main role="main"> 9 <section class="pageTtlSec"> 10 <div class="pageTtlBox"> 11 <h2 class="pageTtl"><span>News</span>お知らせ</h2> 12 </div> 13 </section> 14 15 <ul class="breadcrumb"> 16 <li><a href="/"><span>ホーム</span></a></li> 17 <li><span>お知らせ</span></li> 18 </ul> 19 20 <section class="pageSec"> 21 <div class="inner"> 22 <?php // 年別アーカイブリストを表示 23 $year=NULL; // 年の初期化 24 $args = array( 25 'post_type' => 'news', 26 'orderby' => 'date', 27 'posts_per_page' => -1 28 ); 29 $the_query = new WP_Query($args); if($the_query->have_posts()){ // 投稿があれば表示 30 echo '<ul class="newsChangeBtnList">'; 31 while ($the_query->have_posts()): $the_query->the_post(); // ループの開始 32 if ( $year != get_the_date('Y')){ 33 $year = get_the_date('Y'); 34 echo '<li class="newsChangeBtn"><a href="'.home_url( '/', 'http' ).'news/'.$year.'">'.$year.'</a></li>'; // 年別アーカイブリストの表示 35 } 36 endwhile; 37 echo '</ul>'; 38 wp_reset_postdata(); 39 } 40 ?> 41 <div class="newsBlockList"> 42 <?php 43 $newsTerms = get_terms('news_cat'); 44 if (!empty($newsTerms)): 45 ?> 46 <ul class="newsCategoryList"> 47 <li class="newsCatItem"><a class="catCurrent" href="<?= $gbi; ?>/news/">すべて</a></li> 48 <?php foreach( $newsTerms as $term ): ?> 49 <li class="newsCatItem"><a href="<?= $gbi . '/news/' . $term->slug; ?>/"><?= $term->name; ?></a></li> 50 <?php endforeach; ?> 51 </ul> 52 <?php endif; ?> 53 <section class="newsBlock"> 54 <ol class="newsList"> 55 <?php 56 $args = array( 57 'post_type' => 'news', 58 'posts_per_page' => 10 59 ); 60 $my_query = new WP_Query($args); 61 if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); 62 ?> 63 <li class="newsItem"> 64 <a class="newsLinkBox" href="<?php the_permalink(); ?>"> 65 <span class="newsCategory"> 66 <?php 67 if ($terms = get_the_terms($post->ID, 'news_cat')) { 68 foreach ( $terms as $term ) { 69 echo esc_html($term->name); 70 } 71 } 72 ?> 73 </span> 74 <time class="newsData" datetime="<?php the_time('c'); ?>"><?php the_time('Y.m.d'); ?></time> 75 <p class="newsContent"><?php the_title(); ?></p> 76 </a> 77 </li> 78 <?php endwhile; endif; wp_reset_postdata();?> 79 </ol> 80 81 </section> 82 </div> 83 </div> 84 </section> 85 86</main> 87 88 89<?php get_sidebar(); ?> 90 91<?php get_footer();
■function.php
php
1add_action('init', 'register_post_type_and_taxonomy'); 2function register_post_type_and_taxonomy() { 3 register_post_type( // 4 'news', // 5 array( 6 'labels' => array( 7 'name' => 'お知らせ', 8 'add_new_item' => 'お知らせを新規追加', 9 'edit_item' => 'お知らせの編集', 10 ), 11 'public' => true, 12 'hierarchical' => true, 13 'has_archive' => true, 14 'supports' => array( 15 'title', 16 'editor', 17 'custom-fields' 18 ), 19 'menu_position' => 5, 20 'rewrite' => array('with_front' => false), 21 ) 22 ); 23 24 /* カテゴリタクソノミー */ 25 register_taxonomy( 26 'news_cat', /* タクソノミーの名前 */ 27 'news', /* 使用するカスタム投稿タイプ名 */ 28 array( 29 'hierarchical' => true, 30 'update_count_callback' => '_update_post_term_count', 31 'label' => 'カテゴリー', 32 'singular_label' => 'カテゴリー', 33 'public' => true, 34 'show_ui' => true 35 ) 36 );
ご指摘いただけますでしょうか。よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/14 10:15
2018/06/14 10:23
2018/06/14 10:29
2018/06/14 12:25
2018/06/14 16:41
2018/06/15 02:29
2018/06/15 04:37
2018/06/15 07:07