简体   繁体   English

Wordpress-在index.php上加载自定义CSS

[英]Wordpress - loading custom css on index.php

i am trying to load custom layout style for index.php. 我正在尝试加载index.php的自定义布局样式。 My theme is called kendo and i am trying to pull some custom stylesheets in functions.php 我的主题称为kendo,我正在尝试在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. 您为is_page_template挂钩使用了错误的参数。 Use full template filename with extension. 使用带有扩展名的完整模板文件名。 index.php not index . index.php不是index

See function is_page_template reference. 请参见函数is_page_template参考。

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 另请参阅StackExchange中的两个参考答案: 一个两个

Wouldn't adding this to header.php be easier... 不会将其添加到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 } ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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