サポート » プラグイン » Custom Post Type Permalinksでタームアーカイブのページ送りが404

  • 解決済 mmmpp1111

    (@mmmpp1111)


    お力をお借りしたく、こちらに投稿させていただきます。
    ※例示のURLがスパム判定されてしまうとのことで、httpを抜き、code扱いにしています。

    表題の通り、カスタムタクソノミーに属するタームのアーカイブページでページ送り2ページ目が404になります。

    カスタム投稿タイプ AAA
    カスタムタクソノミー AAA_cat
    ターム BBB,CCC,DDD

    パーマリンク構造

    example.com/AAA/  AAAのアーカイブ
    example.com/AAA/BBB/  タームBBBのアーカイブ
    example.com/AAA/page/2/  AAAのアーカイブのページ送り2ページ目
    

    上記は問題ないのですが、
    example.com/AAA/BBB/page/2/

    以降が404になってしまいます。

    上記パーマリンクを実現するため、プラグイン Custom Post Type Permalinksを使用。
    本来であれば
    タームBBBのアーカイブが
    example.com/AAA_cat/BBB/

    となるのを
    example.com/AAA/BBB/

    にしています。(設定は後述)

    functions.phpに以下の記述でカスタム投稿タイプとカスタムタクソノミーを設定。

    カスタム投稿タイプ

    function create_post_type_column() {
      register_post_type( 'column',
      register_post_type( 'AAA',
        array(
          'label' => 'AAA',
          'labels' => array(
          'all_items' => 'AAA一覧'
          ),
          'public' => true,
          'has_archive' => true,
          'menu_position' => 10,
          'supports' => array( 'title', 'editor', 'author', 'thumbnail')
        )
      );
    }
    add_action( 'init', 'create_post_type_column' );
    

    カスタムタクソノミー

    function add_taxonomy() {
              register_taxonomy(
                'AAA_cat',
                'magazine',
                array(
                  'label' => 'AAAカテゴリ',
                  'singular_label' => 'AAAカテゴリ',
                  'labels' => array(
                  'all_items' => 'AAAカテゴリ一覧',
                  'add_new_item' => 'AAAカテゴリを追加'
                  ),
                  'public' => true,
                  'show_ui' => true,
                  'show_in_nav_menus' => true,
                  'rewrite' => true,
                  'rewrite' => array('slug' => 'AAA'), 
                  'hierarchical' => true
                  )
                );
            }
      add_action( 'init', 'add_taxonomy' );

    パーマリンクを変更するため、Custom Post Type Permalinksで

    exapmle.com/AAA/%AAA_cat%/%postname%/
    has_archive: true / with_front: true

    という設定にし、

    カスタムタクソノミーのアーカイブに、 post_type クエリーを追加。

    にチェックを入れています。

    続いてテンプレートファイルですが、
    example.com/AAA/BBB/
    には、taxonomy-AAA_cat.phpが適応されています。

    taxonomy-AAA_cat.phpの記述です。
    記事取得にはメインループを使用しており、WP_queryなどは使用していません。

    
    <?php if(have_posts()): while(have_posts()): the_post(); ?>
    〜アーカイブ用記事の内容〜
    <?php endwhile; ?><?php else: ?><?php endif; ?>
    //以下ページャー部分
    <?php
                  $big = 9999999999;
                  $arg = array(
                  'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                  'current' => max( 1, get_query_var('paged') ),
                  'total'   => $wp_query->max_num_pages,
                  'prev_text'          => __('< PREV'),
                  'next_text'          => __('NEXT >'),
                  );
                  echo paginate_links($arg);
    ?>

    上記で問題なくページャーの出力はできていますが、いざページ送りをクリックすると、
    example.com/AAA/BBB/page/2/

    が404になるという状態です。

    ページャーの出力は問題なくできていること、
    example.com/AAA/page/2/

    というカスタム投稿タイプのページ送りは
    問題なく表示できることから、
    パーマリンク構造を変更した際のリライトルール周りのエラーだと推測していますが、行き詰まっております。

    試したこと
    https://umi.design/blog/attachment-customtype/

    こちらを参考にfunctions.phpに

    unction chg_qerytaxpage($query) {
        if ( is_admin() || ! $query->is_main_query() ) {
            return;
        }
        if ( $query->is_tax() ) {
            $query->set( 'post_type', 'AAA' );
            $query->set( 'post_mime_type', 'image/jpeg' );
            $query->set( 'post_status', 'inherit' );
            $query->set( 'taxonomy', 'AAA_cat' );
        }
    }
    add_action( 'pre_get_posts', 'chg_qerytaxpage' );

    を記述しても、逆にページに記事が表示されなくなりました。
    http://taneakashi.ad-mk.com/custom-taxonomy-2nd-404.html

    こちらはそもそもWP_Query()を使用していないので当てはまりませんでした。

    足りない情報がございましたらご指摘ください。
    どうぞよろしくお願いいたします。

    wordpress バージョン 5.4.1

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

    (@mmmpp1111)

    AAA_cat部分に/page/2/用のリライトルールを追加することで自己解決しました。

    functions.php

              register_taxonomy(
                'AAA_cat',
                'AAA',
                array(
                  'label' => 'AAAカテゴリ',
                  'singular_label' => 'AAAカテゴリ',
                  'labels' => array(
                  'all_items' => 'AAAカテゴリ一覧',
                  'add_new_item' => 'AAAカテゴリを追加'
                  ),
                  'public' => true,
                  'show_ui' => true,
                  'show_in_nav_menus' => true,
                  'rewrite' => true,
                  'rewrite' => array('slug' => 'AAA'), 
                  'hierarchical' => true
                  )
                );
                add_rewrite_rule('AAA/([^/]+)/page/([0-9]+)/?$', 'index.php?AAA_cat=$matches[1]&paged=$matches[2]', 'top'); //追加部分

    上記を追加しても最初は404は解消せず、
    Custom Post Type Permalinksを一旦停止し、再度有効化(再起動)したところ、
    無事 /AAA/BBB/page/2/ が表示されました。
    同じところで悩んでる方が解消できれば幸いです。

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「Custom Post Type Permalinksでタームアーカイブのページ送りが404」には新たに返信することはできません。