2017-08-09 7 views
1

ページの末尾にクエリvarが追加されたときに新しいページテンプレートを読み込もうとしています。 オリジナルURL:example.com/testpage/ 変数を最後に追加:example.com/testpage/ampWordpress:リライトエンドポイントのクエリvarを使用するページに新しいテンプレートを含める

次に、カスタムPHPテンプレートをロードします。 これはまっすぐ進む動作のようですが、動作させることはできません。 URLに/ amp変数が最後にロードされますが、テンプレートはロードされません。条件 "get_query_var( 'amp') 'を削除すると、問題なくテンプレートがロードされます。私は何が欠けていますか?自分で良い修正が見つかり

add_filter('query_vars', 'register_query_var'); 
function register_query_var($vars) { 
$vars[] = 'amp'; 
    return $vars; 
} 
add_rewrite_endpoint('amp', EP_PAGES); 

add_filter('template_include', 'use_amp_template', 99); 

function use_amp_template($template) { 
global $wp_query; 
if (get_query_var('amp') && is_page()) { 
    $new_template = locate_template(array('amptemplate.php')); 
    if ('' != $new_template) { 
      return $new_template; 
    } 
    } 

    return $template; 
} 

答えて

0

:ありがとう:)

は、ここに私の作業コードです。それが誰にも役立つなら、ここにコードがあります。 ページまたはポストの後ろに 'amp'を追加すると、ページのアンプバージョン用に異なるテンプレートが読み込まれます。 example.com/samplepage/amp または example.com/samplepost/amp

add_filter('query_vars', 'register_query_var'); 
    function register_query_var($vars) { 
    $vars[] = 'amp'; 
     return $vars; 
} 
add_rewrite_endpoint('amp', EP_PAGES | EP_PERMALINK); 


add_filter('template_include', 'use_amp_template', 99); 

function use_amp_template($template) { 
global $wp_query; 

if(isset($wp_query->query['amp']) && is_page()){ 

    $new_template = locate_template(array('amppagetemplate.php')); 
    if ('' != $new_template) { 
     return $new_template; 
    } 
} 

if(isset($wp_query->query['amp']) && is_single()){ 

    $new_template = locate_template(array('ampposttemplate.php')); 
    if ('' != $new_template) { 
     return $new_template; 
    } 
} 

return $template; 
} 
関連する問題