サポート » 使い方全般 » カスタム投稿タイプの年アーカイブのページング

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

    (@yukikaze)

    パーマリンクの設定がおかしいのかと思い試してみました。

    カスタム投稿タイプ:information
    カスタムタクソノミー:info_category
    ターム:A

    表示には、Twenty-oneの
    taxonomy-info_category.php
    archive.php
    を使っています。

    パーマリンク設定の[一般的な設定]で“ /archives/%post_id%/ ”とし、
    Custom Post Permalinksというプラグインで、
    informationを“ /%post_type%/%info_category%/%post_id%/ ”としました。

    結果は、
    ○  http://ドメイン/information/date/2010/
    ○  http://ドメイン/information/date/2010/page/2/
    ×  http://ドメイン/information/A/
    ×  http://ドメイン/information/A/page/2/

    パーマリンク設定の[一般的な設定]で“ /%post_id%/ ”とし、
    Custom Post Permalinksというプラグインで、
    informationを“ /%post_type%/%info_category%/%post_id%/ ”としました。

    結果は、
    ○  http://ドメイン/information/date/2010/
    ×  http://ドメイン/information/date/2010/page/2/
    ○  http://ドメイン/information/A/
    ○  http://ドメイン/information/A/page/2/

    下記の形式全てで表示できるようにするにはどうすれば良いでしょうか。
    http://ドメイン/information/date/2010/
    http://ドメイン/information/date/2010/page/2/
    http://ドメイン/information/A/
    http://ドメイン/information/A/page/2/

    トピック投稿者 yukikaze

    (@yukikaze)

    調べたところ、どなたかのサイトで見たものを function.php に書いた下記の記述で調整できそうなんですが、

    global $my_archives_post_type;
    add_filter( 'getarchives_where', 'my_getarchives_where', 10, 2 );
    function my_getarchives_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 = '';
    	}
    	return $where;
    }
    add_filter( 'get_archives_link', 'my_get_archives_link' );
    function my_get_archives_link( $link_html ) {
    	global $my_archives_post_type;
    	if ( '' != $my_archives_post_type )
    	$add_link .= '?post_type=' . $my_archives_post_type;
    	$link_html = preg_replace("/href=\'(.+)\'\s/","href='$1".$add_link."'",$link_html);
    
    	return $link_html;
    }
    
    // Information年アーカイブ表示変更用(/date/xxxx/?post_type=information -> /information/date/xxxx/)
    function check_custom_rules() {
        global $wp_rewrite, $add_cutom_post_rules;
    
        if ( ! $wp_rewrite->using_permalinks() ) { return; }
        $add_cutom_post_rules = array();
        $rule_templates = array(
            '/'                                                         => '',
            '/([0-9]{1,})/'                                             => '&p=$matches[1]',
            '/page/([0-9]{1,})/'                                        => '&paged=$matches[1]',
            '/date/([0-9]{4})/'                                         => '&year=$matches[1]',
            '/date/([0-9]{4})/page/([0-9]{1,})/'                        => '&year=$matches[1]&paged=$matches[2]',
            '/date/([0-9]{4})/([0-9]{2})/'                              => '&year=$matches[1]&monthnum=$matches[2]',
            '/date/([0-9]{4})/([0-9]{2})/page/([0-9]{1,})/'             => '&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
            '/date/([0-9]{4})/([0-9]{2})/([0-9]{2})/'                   => '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
            '/date/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]{1,})/'  => '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]',
        );
        $post_types = get_post_types( array( 'public' => true, 'show_ui' => true ), false );
    
        if ( $post_types ) {
            foreach ( $post_types as $post_type_slug => $post_type ) {
                if ( ! isset( $post_type->_builtin ) || ! $post_type->_builtin ) {
                    foreach ( $rule_templates as $regex => $rule ) {
                        $add_cutom_post_rules[ $post_type_slug . $regex . '?$' ] = $wp_rewrite->index . '?post_type=' . $post_type_slug . $rule;
                    }
                }
            }
        }
    
        if ( $add_cutom_post_rules ) {
            $rules = $wp_rewrite->wp_rewrite_rules();
            foreach ( $add_cutom_post_rules as $regex => $rule ) {
                if ( ! isset( $rules[$regex] ) ) {
                    $wp_rewrite->flush_rules();
                    break;
                }
            }
        }
    }
    add_action( 'init', 'check_custom_rules' );
    
    function add_custom_type_index_rules( $rules ) {
        global $add_cutom_post_rules;
        if ( $add_cutom_post_rules && is_array( $add_cutom_post_rules ) ) {
            $rules = array_merge( $add_cutom_post_rules, $rules );
        }
        return $rules;
    }
    add_filter( 'rewrite_rules_array', 'add_custom_type_index_rules' );

    このままだと、
    ○  http://ドメイン/information/date/2010/
    ×  http://ドメイン/information/date/2010/page/2/
    ○  http://ドメイン/information/A/
    ○  http://ドメイン/information/A/page/2/
    となります。

    $rule_templates = array(
            '/'                                                         => '',
            '/([0-9]{1,})/'                                             => '&p=$matches[1]',
            '/page/([0-9]{1,})/'                                        => '&paged=$matches[1]',
            '/date/([0-9]{4})/'                                         => '&year=$matches[1]',
            '/date/([0-9]{4})/page/([0-9]{1,})/'                        => '&year=$matches[1]&paged=$matches[2]',
            '/date/([0-9]{4})/([0-9]{2})/'                              => '&year=$matches[1]&monthnum=$matches[2]',
            '/date/([0-9]{4})/([0-9]{2})/page/([0-9]{1,})/'             => '&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
            '/date/([0-9]{4})/([0-9]{2})/([0-9]{2})/'                   => '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
            '/date/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]{1,})/'  => '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]',
        );

    この /date/ の前に information を付け、 /information/date/ とすると、
    ○  http://ドメイン/information/date/2010/
    ○  http://ドメイン/information/date/2010/page/2/
    ×  http://ドメイン/information/A/
    ×  http://ドメイン/information/A/page/2/
    となります。

    お知恵を拝借できないでしょうか。

    トピック投稿者 yukikaze

    (@yukikaze)

    まとめますと、

    カスタム投稿タイプ:information
    カスタムタクソノミー:info_category
    ターム:A

    function.php に下記を記述。

    function check_custom_rules() {
        global $wp_rewrite, $add_cutom_post_rules;
    
        if ( ! $wp_rewrite->using_permalinks() ) { return; }
        $add_cutom_post_rules = array();
        $rule_templates = array(
            '/'                                                         => '',
            '/([0-9]{1,})/'                                             => '&p=$matches[1]',
            '/page/([0-9]{1,})/'                                        => '&paged=$matches[1]',
            '/date/([0-9]{4})/'                                         => '&year=$matches[1]',
            '/date/([0-9]{4})/page/([0-9]{1,})/'                        => '&year=$matches[1]&paged=$matches[2]',
            '/date/([0-9]{4})/([0-9]{2})/'                              => '&year=$matches[1]&monthnum=$matches[2]',
            '/date/([0-9]{4})/([0-9]{2})/page/([0-9]{1,})/'             => '&year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]',
            '/date/([0-9]{4})/([0-9]{2})/([0-9]{2})/'                   => '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]',
            '/date/([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]{1,})/'  => '&year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]',
        );
        $post_types = get_post_types( array( 'public' => true, 'show_ui' => true ), false );
    
        if ( $post_types ) {
            foreach ( $post_types as $post_type_slug => $post_type ) {
                if ( ! isset( $post_type->_builtin ) || ! $post_type->_builtin ) {
                    foreach ( $rule_templates as $regex => $rule ) {
                        $add_cutom_post_rules[ $post_type_slug . $regex . '?$' ] = $wp_rewrite->index . '?post_type=' . $post_type_slug . $rule;
                    }
                }
            }
        }
    
        if ( $add_cutom_post_rules ) {
            $rules = $wp_rewrite->wp_rewrite_rules();
            foreach ( $add_cutom_post_rules as $regex => $rule ) {
                if ( ! isset( $rules[$regex] ) ) {
                    $wp_rewrite->flush_rules();
                    break;
                }
            }
        }
    }
    add_action( 'init', 'check_custom_rules' );
    
    function add_custom_type_index_rules( $rules ) {
        global $add_cutom_post_rules;
        if ( $add_cutom_post_rules && is_array( $add_cutom_post_rules ) ) {
            $rules = array_merge( $add_cutom_post_rules, $rules );
        }
        return $rules;
    }
    add_filter( 'rewrite_rules_array', 'add_custom_type_index_rules' );

    表示には、Twenty Tenの taxonomy-info_category.php, archive.php を使っています。

    パーマリンク設定の[一般的な設定]で“ /%post_id%/ ”とし、 Custom Post Permalinks というプラグインで、 informationを“ /%post_type%/%info_category%/%post_id%/ ”としました。

    結果は、
    ○  http://ドメイン/information/date/2010/
    ×  http://ドメイン/information/date/2010/page/2/
    ○  http://ドメイン/information/A/
    ○  http://ドメイン/information/A/page/2/
    ○  http://ドメイン/date/2011/page/2/?post_type=information

    実現させたいことは、
    http://ドメイン/date/2011/page/2/?post_type=information
    http://ドメイン/information/date/2010/page/2/ で表示できるようにし、

    結果として、下記の形式全てで表示できるようにしたいです。
    http://ドメイン/information/date/2010/
    http://ドメイン/information/date/2010/page/2/
    http://ドメイン/information/A/
    http://ドメイン/information/A/page/2/

    カスタムルールで、
    http://ドメイン/date/2011/page/2/?post_type=information
    http://ドメイン/information/date/2010/page/2/
    リライトする記述をすれば良いのだと思いますが、
    どう書けば良いかわかりません。

    トピック投稿者 yukikaze

    (@yukikaze)

    原因が判明しましたので、別トピックにさせていただきます。

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