前提・実現したいこと
現在、オリジナルのテーマを作成しているのですがカスタム投稿のタームの表示がうまくいきません。
urlは正常に、https://◯◯◯/works_category/◯◯◯/となっているのですが、表示されている一覧を見てみると、通常の一覧が表示されています。
例えばAAAというタームを押すと、AAAカテゴリーだけの一覧ページ、BBBというタームを押すとBBBカテゴリーだけの一覧ページが表示されるようにしたいです。
該当のソースコード
php
1//taxonomy-works_category.php 2 3<ul> 4 <p>カテゴリー</p> 5 <li><a href="<?php echo home_url('/'); ?>works">全て</a></li> 6 <li> 7 <?php 8 $catid = get_the_category('works_category'); 9 $myCat = $catid[0]->cat_ID; 10 wp_list_categories('title_li=&depth=2&hierarchical=0&orderby=ID&order=ASC¤t_category='.$myCat.'&taxonomy=works_category'); 11 ?> 12 </li> 13</ul> 14 15<ul class="col col4 col_sp_full"> 16 <?php 17 $args = array( 18 'post_type' => 'works', 19 'posts_per_page' => 12, 20 'paged' => $paged, 21 'taxonomy' => 'works_category' 22 ); 23 $query = new WP_Query( $args ); 24 ?> 25 26 <?php if ( $query->have_posts() ) : ?> 27 <?php while ( $query->have_posts() ) : $query->the_post();?> 28 29 <li> 30 <a href="<?php the_permalink(); ?>"> 31 <?php if( get_the_post_thumbnail() ) { ?> 32 <div class="post-thumbnail"> 33 <?php the_post_thumbnail(array(360,240)); ?> 34 </div> 35 <?php }else{ ?> 36 <img src="<?php echo get_template_directory_uri(); ?>/images/no-image.jpg"> 37 <?php } ?> 38 39 40 <div class="top-works-text"> 41 <div class="tag"> 42 <span> 43 <?php 44 if ($terms = get_the_terms($post->ID, 'works_category')) { 45 foreach ( $terms as $term ) { 46 echo esc_html($term->name); 47 } 48 } 49 ?> 50 </span> 51 </div> 52 <h3> 53 <?php 54 if(mb_strlen($post->post_title, 'UTF-8')>25){ 55 $title= mb_substr($post->post_title, 0, 25, 'UTF-8'); 56 echo $title.'…'; 57 }else{ 58 echo $post->post_title; 59 } 60 ?> 61 </h3> 62 <p><?php the_excerpt();?></p> 63 </div> 64 </a> 65 </li> 66 67 <?php endwhile; ?> 68 <?php endif; wp_reset_postdata(); ?> 69 70 <div class="pagination-container"> 71 <?php if(function_exists("pagination")) pagination($wp_query->max_num_pages); ?> 72 </div> 73 74 75 </ul>
php
1// functions.php 2 3 4add_action( 'init', 'create_post_type_works' ); 5function create_post_type_works() { 6register_post_type( 'works', //カスタム投稿タイプ名 7array( 8'labels' => array( 9'name' => __( '実績' ), 10'all_items' => __( '実績一覧' ), 11 12), 13'public' => true, 14'has_archive' => true, //アーカイブページを持つ 15'menu_position' =>5, //管理画面のメニュー順位 16'supports' => array( 'title', 'editor', 'thumbnail','excerpt' ), 17) 18); 19/* カテゴリーの設定 */ 20register_taxonomy( 21'works_category', //カテゴリーの名前 22'works', //使うカスタム投稿タイプ名 23array( 24'hierarchical' => true, //trueで親子関係使用 25'update_count_callback' => '_update_post_term_count', 26'label' => 'カテゴリー', 27'singular_label' => 'カテゴリー', 28'public' => true, 29'show_ui' => true 30) 31); 32/* タグを設定 */ 33register_taxonomy( 34'works_tag', //タグの名前 35'works', //使うカスタム投稿タイプ名 36array( 37'hierarchical' => false, 38'update_count_callback' => '_update_post_term_count', 39'label' => 'タグ', 40'singular_label' => 'タグ', 41'public' => true, 42'show_ui' => true 43) 44); 45} 46
試したこと
taxonomy-works_category.phpではWP_Queryの表記が不要とのことだったので、
php
1<?php 2 $args = array( 3 'post_type' => 'works', 4 'posts_per_page' => 12, 5 'paged' => $paged, 6 'taxonomy' => 'works_category' 7 ); 8 $query = new WP_Query( $args ); 9?>
を削除してみたところ、
Fatal error: Uncaught Error: Call to a member function have_posts() on null in /app/public/wp-content/themes/lipronext/taxonomy-works_category.php:55 Stack trace: #0 /app/public/wp-includes/template-loader.php(78): include() #1 /app/public/wp-blog-header.php(19): require_once('/app/public/wp-...') #2 /app/public/index.php(17): require('/app/public/wp-...') #3 {main} thrown in /app/public/wp-content/themes/lipronext/taxonomy-works_category.php on line 55
というエラーが出ました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/27 03:00
2019/08/27 03:10
2019/08/27 03:16