サポート » テーマ » カスタム投稿タイプでのページング

  • 解決済 earlgreyx

    (@earlgreyx)


    こんにちは、よろしくお願いします。

    カスタム投稿タイプ(information)を作って各々の投稿を表示させるところまではテーマ内に作りこみました。
    表示の際に、パーマリンクを /information/投稿ID のようにしたくて、検索して以下のようなコードをカスタム投稿タイプを登録する際、一緒に組み込みました。

    add_action('init','my_rewrite_information');
    add_filter('post_type_link','my_permalink', 1, 3);
    
    function my_rewrite_information()
    {
      global $wp_rewrite;
    
      $queryarg = 'post_type=information&p=';
      $wp_rewrite->add_rewrite_tag('%information_id%', '([^/]+)',$queryarg);
      $wp_rewrite->add_permastruct('information', '/information/%information_id%',array('with_front' => false,'paged' => true));
    }
    
    function my_permalink($post_link, $id = 0, $leavename)
    {
      global $wp_rewrite;
      global $page;
    
      $post = &get_post($id);
      if (is_wp_error($post))
        return $post;
    
      $newlink = $wp_rewrite->get_extra_permastruct($post->post_type);
      $newlink = str_replace('%'.$post->post_type.'_id%', $post->ID, $newlink);
      $newlink = home_url(user_trailingslashit($newlink));
    
      return $newlink;
    }

    普通に表示させる分にはちゃんと表示がされています。
    投稿に本文に<!–nextpage–>を入れて改ページを入れて表示できるように、
    テンプレートに
    <?php wp_link_pages( array( 'before' => '<div class="page-links">' . 'ページ:', 'after' => '</div>' ) ); ?>
    を入れると、ちゃんと分割ページへのリンクが張られるのですが、実際にリンクをクリックすると、
    カスタム投稿タイプ用に作ったテンプレートが読み込まれず、デフォルトのindex.phpがテンプレートとして読み込まれてしまい、「投稿がありません」というメッセージが表示されてしまいます。(※このメッセージは表示すべきデータが無い場合に表示させるように私がテンプレートに書いたものです。)

    分割ページの2ページ目のリンク先は、/information/投稿ID/2 という風になりますが、
    表示は上記のようになります。また、/?post_type=information&p=投稿ID&page=2 というようなURLでアクセスしても同じく表示されません。

    カスタム投稿タイプではなく、普通の投稿であれば、/archives/投稿ID/2 のようにアクセスしても、ちゃんと分割ページが表示されます。

    カスタム投稿タイプで分割ページを扱うには、何か処理が必要なんでしょうか?
    検索すると、カスタム投稿タイプの既存のプラグインの説明が多く、参考になるページが見つかりませんでした。

5件の返信を表示中 - 1 - 5件目 (全5件中)
  • モデレーター gatespace

    (@gatespace)

    パーマリンクを変えるために入れたコードと言うのを削除したらどうなります?
    (/information/タイトル になると思いますが)

    トピック投稿者 earlgreyx

    (@earlgreyx)

    ありがとうございます。

    add_filter,add_actionをコメントアウトしたら、URLが投稿名となり、ページ分割もちゃんと行えるようになりました。

    my_permalink関数の処理がおかしいのだと思います。
    もう少し調べてみます。

    モデレーター gatespace

    (@gatespace)

    余談ですが、私は「Custom Post Type Permalinks」というプラグインを使ってます。

    http://wordpress.org/plugins/custom-post-type-permalinks/
    http://www.torounit.com/blog/tag/custom-post-type-permalinks/

    トピック投稿者 earlgreyx

    (@earlgreyx)

    すみません、以下のadd_rewrite_rule関数を入れることで、自己解決しました。

    add_action('init','my_rewrite_information');
    function my_rewrite_information()
    {
      global $wp_rewrite;
    
      add_rewrite_rule("information/([^/]+)/([0-9]{1,})$",'index.php?post_type='.$post_type.'&p=$matches[1]&page=$matches[2]','top');  
    
      $queryarg = 'post_type=information&p=';
      $wp_rewrite->add_rewrite_tag('%information_id%', '([^/]+)',$queryarg);
      $wp_rewrite->add_permastruct('information', '/information/%information_id%',array('with_front' => false,'paged' => true));
    }

    ただ、理屈が分かりませんが・・・。

    トピック投稿者 earlgreyx

    (@earlgreyx)

    すみません、追加した行に間違いありました。
    正しくは下記です。

    add_rewrite_rule("information/([^/]+)/([0-9]{1,})$",'index.php?post_type=information&p=$matches[1]&page=$matches[2]','top');

5件の返信を表示中 - 1 - 5件目 (全5件中)
  • トピック「カスタム投稿タイプでのページング」には新たに返信することはできません。