2015-05-29 56 views
0

我想加載自定義佈局樣式的index.php。我的主題叫做劍道,我試圖在functions.php中抽出一些自定義樣式表。WordPress的 - 加載自定義CSS在index.php

樣式表單獨運行(例如,如果我刪除一行),但它們保持相互覆蓋。我的代碼有問題嗎?

function kendo_scripts() { 
    wp_enqueue_style('kendo-style', get_stylesheet_uri()); 

    if (is_page_template('index')) { 
     wp_enqueue_style('kendo-layout-style1', get_template_directory_uri() . '/layouts/no-sidebar.css'); 
    } 
    if (!is_page_template('index')) { 
     wp_enqueue_style('kendo-layout-style2', get_template_directory_uri() . '/layouts/content-sidebar.css'); 
    } 
+0

請詳細說明你正在努力實現。 – Nilambar

回答

0

您正在使用is_page_template掛鉤的錯誤參數。使用擴展名的完整模板文件名。 index.php而不是index

See function is_page_template reference.

這是可能存在的問題。示例代碼:

/*Add Hook*/ 
add_action('wp_enqueue_scripts', 'kendo_scripts'); 

/*Define the callback*/ 
function kendo_scripts() { 

    wp_enqueue_style('kendo-style', get_stylesheet_uri(), array(), null, 'all'); 

    if (is_page_template('index.php')) { 

     wp_enqueue_style('kendo-layout-style1', get_template_directory_uri() .'/css/bootstrap.min.css', array(), null, 'all'); 

    } else { 

     wp_enqueue_style('kendo-layout-style2', get_template_directory_uri() . '/layouts/content-sidebar.css', array(), null, 'all'); 
    } 
} 

又見StackExchange 2個參考答案:onetwo

+0

謝謝你,我試着用索引和index.php參數,但它仍然沒有工作。我已經通過使用不同的CSS類,這可能不是理想的,但它的工作... –

0

會不會加入這header.php中更容易...

<?php if (is_front_page()) { ?> 
<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/css/stylesheet1.css"/> 
<?php } else { ?> 
<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_directory'); ?>/css/stylesheet2.css"/> 
<?php } ?> 
+0

謝謝,生病嘗試這種方式也。 –