前提・実現したいこと
オリジナルのWordPressテーマを作ってるのですが、その中のカスタム投稿でカテゴリ別の記事一覧ページを表示したいです。
archive-works.phpというファイルの上部にカテゴリーが並んでいるのですが、例えばイベントを押すとイベントの全ての記事を表示、お知らせを押すとお知らせの全ての記事を表示といった仕組みにしたいのです。カスタム投稿ではないarchive.phpというファイルでは実装できたのですが、カスタム投稿でうまく表示されず困っています。
現在、archive-works.phpでは下の画像のように、archive.phpで読み込んだcategoryが表示されています。
うまくworks_categoryが読み込まれていないことが原因だとは思うのですが。。。
足りない情報などありましたら教えてください( ; ; )
お手数おかけしますが回答お待ちしております。
該当のソースコード
php
1 2 3// archive-works.php 4 5<ul> 6 <p>カテゴリー</p> 7 <li><a href="<?php echo home_url('/'); ?>works">全て</a></li> 8 <li> 9 <?php 10 $catid = get_the_category('works_category'); 11 $myCat = $catid[0]->cat_ID; 12 wp_list_categories('title_li=&depth=2&hierarchical=0&orderby=ID&order=ASC¤t_category='.$myCat); 13 ?> 14 </li> 15</ul>
php
1 2// category-works.php 3 4<ul> 5 <?php 6 $posts = get_posts(array( 7 'post_type' => 'works', 8 'posts_per_page' => 12, 9 'paged' => $paged, 10 'category' => $cat // ここでIDを取得 11 )); 12 ?> 13 14 <?php if($posts): foreach($posts as $post): setup_postdata($post); ?> 15 16 <li> 17 <a href="<?php the_permalink(); ?>"> 18 19 <div class="news-archive-flex"> 20 <div class="news-archive-img"> 21 <?php if( get_the_post_thumbnail() ) { ?> 22 <div class="post-thumbnail"> 23 <?php the_post_thumbnail(); ?> 24 </div> 25 <?php }else{ ?> 26 <img src="<?php echo get_template_directory_uri(); ?>/images/no-image.jpg"> 27 <?php } ?> 28 </div> 29 30 <div class="top-works-text"> 31 <div class="tag"> 32 <span> 33 <?php $cat = get_the_category(); ?> 34 <?php $cat = $cat[0]; ?> 35 <?php echo get_cat_name( $cat->term_id); ?> 36 </span> 37 </div> 38 39 <span class="date"><?php the_time( 'Y.m.d' ); ?></span> 40 41 <h3> 42 <?php 43 if(mb_strlen($post->post_title, 'UTF-8')>30){ 44 $title= mb_substr($post->post_title, 0, 30, 'UTF-8'); 45 echo $title.'…'; 46 }else{ 47 echo $post->post_title; 48 } 49 ?> 50 </h3> 51 <p><?php echo mb_substr( get_the_excerpt(), 0, 200 ) ; ?></p> 52 </div> 53 54 55 </div> 56 </a> 57 </li> 58 59 <?php endforeach; endif; ?> 60 61 <div class="pagination-container"> 62 <?php if(function_exists("pagination")) pagination($wp_query->max_num_pages); ?> 63 </div> 64 65 </ul>
php
1 2// functions.php 3 4 5add_action( 'init', 'create_post_type_works' ); 6function create_post_type_works() { 7register_post_type( 'works', //カスタム投稿タイプ名 8array( 9'labels' => array( 10'name' => __( '実績' ), 11'all_items' => __( '実績一覧' ), 12 13), 14'public' => true, 15'has_archive' => true, //アーカイブページを持つ 16'menu_position' =>5, //管理画面のメニュー順位 17'supports' => array( 'title', 'editor', 'thumbnail','excerpt' ), 18) 19); 20/* カテゴリーの設定 */ 21register_taxonomy( 22'works_category', //カテゴリーの名前 23'works', //使うカスタム投稿タイプ名 24array( 25'hierarchical' => true, //trueで親子関係使用 26'update_count_callback' => '_update_post_term_count', 27'label' => 'カテゴリー', 28'singular_label' => 'カテゴリー', 29'public' => true, 30'show_ui' => true 31) 32); 33/* タグを設定 */ 34register_taxonomy( 35'works_tag', //タグの名前 36'works', //使うカスタム投稿タイプ名 37array( 38'hierarchical' => false, 39'update_count_callback' => '_update_post_term_count', 40'label' => 'タグ', 41'singular_label' => 'タグ', 42'public' => true, 43'show_ui' => true 44) 45); 46} 47 48
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。