2017-01-12 3 views
0

でカスタムシングルポストテンプレートを確認するには、私はそれは、単一のポストのためsingle-1.phpテンプレートを呼び出しているデフォルトを一single.phpどのように条件付きのWordPress

function call_different_single_post($single_template) 
{ 
    global $post; 

    $path = get_stylesheet_directory() . '/includes/templates' . '/'; 
    $single_template = $path. 'single-1.php'; 
    return $single_template; 
} 

add_filter('single_template', 'call_different_single_post'); 

をオーバーライドするカスタム単一のポストを呼び出すために、次の機能を使用しています。

は今、私は

function call_cust_js_single(){ 

    if(/*..this is single-1.php template..*/){ 
    wp_register_script('cust-js', get_stylesheet_directory_uri() . 'custom.js', false, '1.0.0'); 
    wp_enqueue_script('cust-js'); 

    } 

} 
add_action('wp_enqueue_scripts', 'call_cust_js_single'); 
+0

* single-1.php *ファイルにカスタム 'post_type'を表示していますか? –

+0

'is_singular( 'you_custom_post_type');' - 参照 - https://codex.wordpress.org/Function_Reference/is_singular – htmlbrewery

+0

を使用しようとしていません。それはカスタム投稿タイプでもカスタム投稿タイプでもありません。あなたが見ることができるように、single-1.phpは/ includes/templatesフォルダーにあり、そこから私はこのフィルターsingle_templateでデフォルトのsingle.phpファイルをオーバーライドするよう呼びかけています。これを適用すると、デフォルトのblog single.phpがsingle-1.phpに置き換えられます。しかし、これを条件付きでチェックする方法がわからない場合は、 – Ayanize

答えて

1

あり、これに類似した記事だが、それは非常にあなたの要求をカバーしていないようないくつかの他のjsファイルを呼び出すことができるように、条件付きで、このテンプレートを確認したいです。ここでは、リンクがhere

だここで貴重なコードは次のとおりです。

add_filter('template_include', 'var_template_include', 1000); 
function var_template_include($t){ 
    $GLOBALS['current_theme_template'] = basename($t); 
    return $t; 
} 

function get_current_template($echo = false) { 
    if(!isset($GLOBALS['current_theme_template'])) 
     return false; 
    if($echo) 
     echo $GLOBALS['current_theme_template']; 
    else 
     return $GLOBALS['current_theme_template']; 
} 

今、あなたはテンプレートファイルのファイル名を返す関数get_current_templateを持っています。

今すぐあなたのheader.phpのファイルを編集し、ここでの例です:

<?php if('single-1.php' == get_current_template()) 
    { 
     //load your scripts here 
    } ?> 

は、それはあなたが探していたもののようにきれいではないのですが、私はそれはあなたを助けると思います。

関連する問題