###前提
下記「functions.php」のように、パーマリンクにリダイレクトをかけました。
これによって、
「example.com/country/japan/history/」へのアクセス時に、
「post-country-history.php」が読み込まれ、リダイレクトは正常に作動しています。
###問題
しかしそのアクセス時に、下記「header.php」の処理において、if( is_404() ){}
に該当してしまいます。
つまり'404です'
という文字列がcookieに保存されてしまうのです。
###実現したいこと
「example.com/country/japan/history/」のような、リダイレクト可能なページにアクセスしたときに、if( is_404() ){}
に該当させない方法を知りたいです。
###「functions.php」
こちらで、リダイレクトは正常に作動する状態です。
php
1// リライトルールを指定 2function rewrite_init() { 3 add_rewrite_rule( 4 '^country/([^/]+)/(history|character)?$', 5 'index.php?country=$matches[1]&countrychild=$matches[2]', 6 'top' 7 ); 8} 9add_action( 'init', 'rewrite_init' ); 10 11// 子ページのルールを追加 12function rewrite_query_vars_perf( $query_vars ) { 13 $query_vars[] = 'countrychild'; 14 return $query_vars; 15} 16add_action( 'query_vars', 'rewrite_query_vars_perf' ); 17 18// 子ページで使うテンプレートを指定 19function rewrite_template_include_perf( $template ) { 20 if ( get_query_var( 'post_type' ) == 'country' ) { 21 $child = get_query_var( 'countrychild' ); 22 if ( $child == 'history' ) { 23 $template = locate_template( 'post-country-history.php' ); 24 } else if ( $child == 'character' ) { 25 $template = locate_template( 'post-country-character.php' ); 26 } 27 } 28 return $template; 29} 30add_filter( 'template_include', 'rewrite_template_include_perf' );
###「header.php」
リダイレクトは正常に作動する状態にも関わらず、下記の処理において、if( is_404() ){}
に該当してしまうのが問題です。
php
1<head> 2 <?php 3 if( is_404() ){ 4 echo "<script>$.cookie('page', '404です');</script>"; 5 }else{ 6 echo "<script>$.cookie('page', 'OKです');</script>"; 7 } 8 ?> 9 <?php wp_head(); ?> 10</head>
###補足
「example.com/country/japan/history/」へのアクセス時が問題でして、
「example.com/country/japan/」は通常通りアクセスできます。
あなたの回答
tips
プレビュー