サポート » 使い方全般 » カスタム投稿の年別アーカイブについて

  • オリジナルのテーマでカスタム投稿を作成しました。
    single-投稿名.phpの個別記事と、archive-投稿名.phpの一覧ページは表示できました。

    年別のアーカイブページはarchive-投稿名.phpでは表示できないのでしょうか?

    <?php wp_get_archives('type=yearly&post_type=投稿名'); ?>

    で年別のリストは表示できたのですが、リストのリンク先に飛ぶとその年の記事一覧が表示されず真っ白になってしまいます。

    functions.phpには以下のように記述していますがどこか間違っているでしょうか?
    よろしくお願い致します。

    /* カスタム投稿設定 */
    add_action( 'init', 'create_post_type' );
    function create_post_type() {
      register_post_type( '投稿名',
        array(
          'labels' => array(
          'name' => __( 'ラベル' ),
          'singular_name' => __( 'ラベル' )
          ),
          'public' => true,
          'has_archive' => true,
          'rewrite' => true,
          'menu_position' => 5,
          'supports' => array('title','editor','thumbnail',
          'custom-fields','excerpt','author','trackbacks',
          'comments','revisions','page-attributes')
        )
      );
    }
    
    /* スラッグ変更 */
    function add_slug_for_posts($post_id) {
    global $wpdb;
    
    $posts_data = get_post($post_id, ARRAY_A);
    $slug = $posts_data['post_name'];
    
    if ($post_id != $slug){
      $my_post = array();
      $my_post['ID'] = $post_id;
      $my_post['post_name'] = $post_id;
    wp_update_post($my_post);
    }
    
    }
    add_action('publish_voice', 'add_slug_for_posts');
    
    /* 年別アーカイブ */
    function add_custom_rewrite_rules() {
        $rules = array(
            'top' => array(
                '^event/([0-9]{4})/page/([0-9]+)/?' => 'index.php?post_type=event&year=$matches[1]&paged=$matches[2]',
                '^event/([0-9]{4})/?' => 'index.php?post_type=event&year=$matches[1]',
            ),
            'bottom' => array(
            )
        );
        foreach ( $rules as $position => $position_rules ) {
            foreach ( $position_rules as $rule => $rewrite ) {
                add_rewrite_rule($rule, $rewrite, $position );
            }
        }
    }
    add_action( 'init', 'add_custom_rewrite_rules' );
    
    function get_post_type_archives_where( $where, $r ) {
        global $my_archives_post_type;
        if ( isset( $r['post_type'] ) ) {
            $my_archives_post_type = $r['post_type'];
            $where = str_replace( '\'post\'', '\'' . $r['post_type'] . '\'', $where );
        } else {
            $my_archives_post_type = false;
        }
        return $where;
    }
    add_filter( 'getarchives_where', 'get_post_type_archives_where', 10, 2 );
    
    function get_post_type_archives_link( $link_html ) {
        global $my_archives_post_type;
        if ( $my_archives_post_type ) {
            $link_html = preg_replace( "#(/[0-9]{4})#", '/' . $my_archives_post_type . '$1', $link_html);
            $link_html = preg_replace( "#>([0-9]{4})</a>#", '>$1年</a>', $link_html);
        }
        return $link_html;
    }
    add_filter( 'get_archives_link', 'get_post_type_archives_link' );
  • トピック「カスタム投稿の年別アーカイブについて」には新たに返信することはできません。