2015-05-29 55 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

谢谢,生病尝试这种方式也。 –