简体   繁体   中英

Wordpress - loading custom css on index.php

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

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

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 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM