Wordpress - loading custom css on index.php

2019-09-01 09:55发布

i am trying to load custom layout style for index.php. My theme is called kendo and i am trying to pull some custom stylesheets in functions.php

Stylesheets work separately (if i delete one line for example), but they keep overriding each other. Is something wrong with my code?

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' );
    }

2条回答
孤傲高冷的网名
2楼-- · 2019-09-01 10:32

You are using wrong parameter for is_page_template hook. Use full template filename with extension. index.php not index.

See function is_page_template reference.

This was the possible issues. A sample code:

/*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' );
   }
}

See also 2 reference answers in StackExchange: one and two

查看更多
Rolldiameter
3楼-- · 2019-09-01 10:38

Wouldn't adding this to header.php be easier...

<?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 } ?>
查看更多
登录 后发表回答