category.php内で以下の様に書いて一応はうまく動いているのですが、不格好なため修正したいです。
php
1<?php if (is_category('cat03-1')){//修正したい箇所ここから 2 $cats = 'cat03-1'; 3 }elseif (is_category('cat03-2')){ 4 $cats = 'cat03-2'; 5 }; 6echo '<p>'.$cats.'</p>';//確認のため 7//ここまで 8?> 9 10<?php 11$paged = get_query_var('page'); 12$my_query = new WP_Query(array( 13'post_type' => 'works', 14'category_name' => $cats, 15'posts_per_page' => -1, 16'orderby' => 'date', 17'post_status' => 'publish', 18'paged' => $paged 19)); 20?>
functions.php にカスタム投稿タイプを追加して、普通の投稿とカテゴリーやタグを共有しているせいか、
普通にcategory.phpを書いても何を表示してくれません。
(category.php の例:https://noumenon-th.net/programming/2016/06/19/wordpress06/)
functions.php には以下の様に記載しています。
php
1//カスタムポストタイプ 活動 を追加する 2add_action( ‘init’, ‘create_post_type03’ ); 3function create_post_type03() { 4register_post_type( 5‘works’, /* 投稿タイプのslug */ 6array( 7‘labels’ => array( 8‘name’ => __( ‘活動’ ), 9‘singular_name’ => __( ‘活動’ ) 10), 11‘taxonomies’ => array( ‘category’, ‘post_tag’ ), 12‘public’ => true, 13‘menu_position’ => 5, 14‘has_archive’ => true, 15‘rewrite’ => array( ‘with_front’ => false ), 16‘supports’ => array(‘title’,’editor’,’thumbnail’,’custom-fields’,’excerpt’,’author’,’trackbacks’,’comments’,’revisions’,’page-attributes’) 17) 18); 19} 20//通常ポストとカスタム投稿タイプでタグを共有する 21register_taxonomy(‘post_tag’, array(‘post’,’works’), array( //ポイント 22‘hierarchical’ => true, 23‘update_count_callback’ => ‘_update_post_term_count’, 24‘label’ => ”, 25‘singular_label’ => ”, 26‘public’ => true, 27‘show_ui’ => true 28) 29); 30function add_post_tag_archive( $wp_query ) { 31if ($wp_query->is_main_query() && $wp_query->is_tag()) { 32$wp_query->set( ‘post_type’, array(‘post’,’works’)); //ポイント 33} 34} 35add_action( ‘pre_get_posts’, ‘add_post_tag_archive’ , 10 , 1);
get_the_category();等試したのですがうまく行きませんでした。
上手な書き方を教えてください。
また、これはcategory.phpを使用するので正解でいいのでしょうか?
よろしくお願いいたします。

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/05 01:07