• 解決済 himuhimu77

    (@himuhimu77)


    カスタム投稿のタクソノミーアーカイブページが表示されません。

    functions.phpで

    // カスタム投稿タイプを定義
    add_action( 'init', 'register_cpt_sample_custom' );
     
    function register_cpt_sample_custom() {
     
        $labels = array(
            'name' => _x( 'カスタム投稿', 'sample_custom' ),
            'singular_name' => _x( 'カスタム投稿', 'sample_custom' ),
            'add_new' => _x( '新規追加', 'sample_custom' ),
            'add_new_item' => _x( '新しいカスタム投稿プロフィールを追加', 'sample_custom' ),
            'edit_item' => _x( 'カスタム投稿プロフィールを編集', 'sample_custom' ),
            'new_item' => _x( '新しいカスタム投稿', 'sample_custom' ),
            'view_item' => _x( 'カスタム投稿プロフィールを見る', 'sample_custom' ),
            'search_items' => _x( 'カスタム投稿検索', 'sample_custom' ),
            'not_found' => _x( 'プロフィールが見つかりません', 'sample_custom' ),
            'not_found_in_trash' => _x( 'ゴミ箱にプロフィールはありません', 'sample_custom' ),
            'parent_item_colon' => _x( '親カスタム投稿:', 'sample_custom' ),
            'menu_name' => _x( 'カスタム投稿', 'sample_custom' ),
        );
     
        $args = array(
          'labels' => $labels,
          'hierarchical' => true,
          'description' => '説明',
          'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields' ),
          'public' => true,
          'show_ui' => true,
          'show_in_menu' => true,
          'show_in_rest' => true,
          'show_in_nav_menus' => true,
          'publicly_queryable' => true,
          'exclude_from_search' => false,
          'has_archive' => true,
          'query_var' => true,
          'can_export' => true,
          'rewrite' => true,
          'capability_type' => 'post',
          'menu_icon' => 'dashicons-businessman',
        );
     
        register_post_type( 'sample_custom', $args );
    
    
        register_taxonomy(
          'custom_name',   
          'sample_custom',  
          array(
            'label' => 'タクソノミー1',  
            'labels' => array(
              'popular_items' => 'よく使うタクソノミー1',
              'edit_item' =>'タクソノミー1を編集',
              'add_new_item' => '新規タクソノミー1を追加',
              'search_items' => 'タクソノミー1を検索'
            ),
            'public' => true,  
            'description' => 'タクソノミー1の説明文です。',  
            'hierarchical' => true, 
            'show_in_rest' => true,  
            'show_ui' => true,
          'rewrite' => array(
              'slug' => 'sample_custom','with_front' => true,'hierarchical' => true)
          )
        );
    
        register_taxonomy(
          'custom_name_2',  
          'sample_custom',   
          array(
            'label' => 'タクソノミー2', 
            'labels' => array(
              'popular_items' => 'よく使うタクソノミー2',
              'edit_item' =>'タクソノミー2を編集',
              'add_new_item' => '新規タクソノミー2を追加',
              'search_items' => 'タクソノミー2を検索'
            ),
            'public' => true, 
            'description' => 'タクソノミー2の説明文です。',  
            'hierarchical' => true, 
            'show_in_rest' => true,
            'show_ui' => true,
          'rewrite' => array(
              'slug' => 'sample_custom','with_front' => true,'hierarchical' => true)
          )
        );
    
    }

    上記のコードでカスタム投稿タイプと、それに属するカスタムタクソノミーを二つ作成しました。

    こちらのコードで、カスタム投稿タイプの一覧ページは
    http://example.com/sample_custom/
    で表示されています。が、このカスタム投稿のタイプのカスタムタクソノミーの一覧ページをtaxonomy.phpを作成して表示しようとしても、404エラーになってしまいます。

    パーマリンクの更新や、テーマを変えて試してみたのですが結果は同じです。

    カスタム投稿のタイプのカスタムタクソノミーの一覧ページのurlは、私の場合ですとそれぞれ
    http://example.com/sample_custom/custom_name/タームのスラッグ/
    http://example.com/sample_custom/custom_name_2/タームのスラッグ/
    だと認識しておりますが、これが間違っているのでしょうか。

    試しに表示できているカスタム投稿タイプの一覧ページで

       <?php
      $terms = get_the_terms($post->ID, ‘custom_name');
      foreach($terms as $term) {
        $link = get_term_link($term->slug, $term->taxonomy);
        echo $link;
      }
    ?>

    上記を実行してみたところ、

    画面には
    http://example.com/custom_name_2/タームのスラッグ/
    と表示されました。が、こちらのURLも飛ぶと404エラーになってしまいます。他に何か試すことはございますでしょうか?お力添えいただけると嬉しいです。

    ちなみに今のところはlocalというソフトを使ってローカル上でサイトを作っておりまして、まだ本番環境での作業はできていない状態です。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 himuhimu77

    (@himuhimu77)

    こちら自己解決いたしました。functions.phpの

    'rewrite' => array(
         'slug' => 'sample_custom','with_front'  =>true,'hierarchical' => true)
         

    上記の部分を全て削除したところ、
    http://example.com/custom_name/タームのスラッグ/
    のURLでタクソノミーアーカイブページが表示されるようになりました。

    上記で、タクソノミーアーカイブページのURLが
    http://example.com/sample_custom/custom_name/タームのスラッグ/
    となると思っていたのですが、何かがうまくいっていなかったようです。その部分に関してはまた検証するとして、とりあえずページが表示できるようになることが最優先でしたので、一旦ここで解決といたします。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「タクソノミーアーカイブページが表示されない」には新たに返信することはできません。