サポート » 使い方全般 » カスタム投稿タイプ+カスタム分類(階層有)の子カテゴリーのパーマリ

  • いつもお世話になっております。
    カスタム投稿タイプ+カスタム分類(階層有)で子カテゴリーを作成した際に、パーマリンクの挙動がおかしくなるので質問させていただきます。

    ※カスタム投稿タイプ・カスタム分類でのパーマリンクは数々のトピックや紹介されているブログを拝見し、その上で自己解決に至らなかったので新規トピック立てしました。
    内容的に重複していましたらご容赦ください…。

    まず、現在の状況ですが、functions.phpに以下の記述を行なっております。

    function add_test_type() {
        $labels = array(
            'name' => 'テスト投稿タイプ',
            'singular_name' => 'テスト投稿タイプ',
            'menu_name' => 'テスト投稿タイプ',
            'add_new' => '新規追加',
            'add_new_item' => '新規追加',
            'edit' => '編集',
            'edit_item' => '編集',
            'new_item' => '新規追加',
            'view' => '表示',
            'view_item' => '表示',
            'search_items' => '検索',
            'not_found' => '見つかりませんでした。',
            'not_found_in_trash' => 'ゴミ箱は空です。',
            'parent' => '親',
        );
        $args = array(
            'labels' => $labels,
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'capability_type' => 'post',
            'hierarchical' => false,
            'query_var' => true,
            'has_archive' => true,
            'exclude_from_search' => false,
            'supports' => array(
                'title',
                'editor',
                'thumbnail',
                'revision',
            ),
        );
        register_post_type('test', $args);
    
        $args = array(
            'label' => 'テスト分類',
            'public' => true,
            'show_ui' => true,
            'query_var' => true,
            'hierarchical' => true,
        );
        register_taxonomy('tests', 'test', $args);
    }
    add_action('init', 'add_test_type');

    で、パーマリンク設定には、Custom Post Parmalinkを採用し、

    /%post_type%/%tests%/%test%.html

    のように設定しています。
    通常のパーマリンク設定は、

    /%category%/%postname%.html

    となっています。

    で、カスタム分類には以下のようにカテゴリー登録しています。()内はSlugです。

    親カテゴリーA(cat-a)
    ├ 子カテゴリーA(sub-a)
    └ 子カテゴリーB(sub-b)
     └ 孫カテゴリー(last)
    親カテゴリーB(cat-b)

    次に、現状のURL表示状況ですが、

    http://sample.com/ ← トップページ(○)
    http://sample.com/test/ ← 全てのアーカイブ(○)
    http://sample.com/test/cat-a/ ← 親カテゴリーAのアーカイブ(○)
    http://sample.com/test/cat-a/sub-a/ ← 子カテゴリーAのアーカイブ(×)
    http://sample.com/test/cat-a/sub-b/ ← 子カテゴリーBのアーカイブ(×)
    http://sample.com/test/cat-a/sub-b/last ← 孫カテゴリーのアーカイブ(×)
    http://sample.com/test/cat-b/ ← 親カテゴリーBのアーカイブ(○)/blockquote>

    となります。ここで、現在(×)になっている項目ですが、下記のURLであれば認識されます。

    http://sample.com/test/sub-a/ ← 子カテゴリーAのアーカイブ(○)
    http://sample.com/test/sub-b/ ← 子カテゴリーBのアーカイブ(○)
    http://sample.com/test/last/ ← 孫カテゴリーのアーカイブ(○)

    通常の投稿でカテゴリーを親子関係を持たせた場合であれば、キチンと表示されますが、カスタム投稿タイプ+カスタム分類での実現はできないのでしょうか?
    ちなみに、上記で(×)となっているようなURL構造をそのまま、パンくずリストでも表現したいのですが、うまくいきません…。

    現状、functions.phpに記述しているパンくず用の関数は以下の通りです。

    function BreadCrumbsList(){
    	global $wp_query;
    	$separate = '<span class="separate">></span>';
    	$bc_list_num = 0;
    	if(!is_home() && !is_front_page()) {
    		$front_page_ID = get_option('page_on_front');
    		if($front_page_ID != 0) {
    			$front_page_title = get_the_title($front_page_ID);
    		}else {
    			$front_page_title = 'トップページ';
    		}
    		echo '<ul class="breadcrumbs"><li class="first"><a href="'. get_bloginfo('url') .'">'. $front_page_title .'</a></li>';
    		if(is_category()) {
    			$bc_cat = get_category(get_query_var('cat'));
    			while($bc_cat->cat_ID != 0) {
    				$bc_list_name[$bc_list_num] = $bc_cat->cat_ID;
    				$bc_cat = get_category($bc_cat->parent);
    				$bc_list_num++;
    			}
    			while($bc_list_num != 0) {
    				$bc_list_num--;
    				if($bc_list_num == 0) {
    					echo '<li class="last">' . $separate . get_catname($bc_list_name[$bc_list_num]) . '</li>';
    				}else {
    					echo '<li>' . $separate . '<a href="' . get_category_link($bc_list_name[$bc_list_num]) . '" title="' . get_catname($bc_list_name[$bc_list_num]) . 'を表示">' . get_catname($bc_list_name[$bc_list_num]) . '</a></li>';
    				}
    			}
    		}elseif(is_year()) {
    			echo '<li class="last">' . $separate . get_the_time('Y') . '年</li>';
    		}elseif(is_month()) {
    			echo '<li><a href="'. get_bloginfo('url') . '/date/' . get_the_time('Y') .'">' . $separate . get_the_time('Y') . '年</a></li>';
    			echo '<li class="last">' . $separate . get_the_time('n') . '月</li>';
    		}elseif(is_day()) {
    			echo '<li><a href="'. get_bloginfo('url') . '/date/' . get_the_time('Y') .'">' . $separate . get_the_time('Y') . '年</a></li>';
    			echo '<li><a href="'. get_bloginfo('url') . '/date/' . get_the_time('Y') . '/' . get_the_time('n') .'">' . $separate . get_the_time('n') . '月</a></li>';
    			echo '<li class="last">' . $separate . get_the_time('j') . '日</li>';
    		}elseif(is_tag()) {
    			echo '<li class="last">' . $separate . single_tag_title('',false) . '</li>';
    		}elseif(is_archive() && !is_category()) {
    			echo '<li class="last">' . $separate . __('Archives') . '</li>';
    		}elseif(is_single()) {
    			$bc_cates = get_the_category();
    			foreach($bc_cates as $bc_fastcate) {
    				if($bc_fastcate->category_parent > 0) {
    					$bc_cat = get_category($bc_fastcate);
    					while($bc_cat->cat_ID != 0) {
    						$bc_list_name[$bc_list_num] = $bc_cat;
    						$bc_cat = get_category($bc_cat->parent);
    						$bc_list_num++;
    					}
    					foreach(array_reverse($bc_list_name) as $bc_list_num) {
    						echo '<li>' . $separate . '<a href="' . get_category_link($bc_list_num->cat_ID) . '" title="' . get_catname($bc_list_num->cat_ID) . 'を表示">' . get_catname($bc_list_num->cat_ID) . '</a></li>';
    					}
    						echo '<li class="last">' . $separate . mb_strimwidth(the_title('','', FALSE), 0, 46, "(略)", 'UTF-8') . '</li>';
    				}elseif($bc_fastcate->category_parent == 0) {
    					echo '<li>' . $separate . '<a href="' . get_category_link($bc_fastcate->cat_ID) . '" title="' . get_catname($bc_fastcate->cat_ID) . 'を表示">' . get_catname($bc_fastcate->cat_ID) . '</a></li>';
    					echo '<li class="last">' . $separate . mb_strimwidth(the_title('','', FALSE), 0, 46, "(略)", 'UTF-8') . '</li>';
    				}
    				break;
    			}
    		}elseif(is_search()){
    			echo '<li class="last">' . $separate . __('Search Results') . '</li>';
    		}elseif(is_404()) {
    			echo '<li class="last">' . $separate . __('Page not found') . '</li>';
    		}elseif (is_page()) {
    			$bc_post = $wp_query->get_queried_object();
    			if($bc_post->post_parent == 0) {
    				echo '<li class="last">' . $separate . the_title('','', FALSE).'</li>';
    			}else {
    				$bc_title = the_title('','', FALSE);
    				$bc_ancestors = array_reverse(get_post_ancestors($bc_post->ID));
    				array_push($bc_ancestors, $bc_post->ID);
    				foreach($bc_ancestors as $bc_ancestor) {
    					if($bc_ancestor != end($bc_ancestors)) {
    						echo '<li>' . $separate . '<a href="'. get_permalink($bc_ancestor) .'">'. strip_tags(apply_filters('single_post_title', get_the_title($bc_ancestor))) .'</a></li>';
    					}else {
    						echo '<li class="last">' . $separate . strip_tags(apply_filters('single_post_title', get_the_title($bc_ancestor))) .'</li>';
    					}
    				}
    			}
    		}
    		echo "</ul>";
    	}
    }

    一応、投稿、ページ、各種アーカイブ全てに現状では対応させていました。
    カテゴリーに階層に合わせたパンくず出力が出来ていたので良かったのですが、今回のカスタム投稿タイプ+カスタム分類で思いっきりNGでした(’・ω・`;)
    ※当方、PHP経験がないので、もっと良い方法があるのかもしれませんが…。ご指摘あればコチラも合わせてよろしくお願い致します。

    長くなってしまいましたが以上の状況で何か良い解決策がありましたらご教授ください。
    よろしくお願い致します。

  • トピック「カスタム投稿タイプ+カスタム分類(階層有)の子カテゴリーのパーマリ」には新たに返信することはできません。