簡體   English   中英

Wordpress-在index.php上加載自定義CSS

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

我正在嘗試加載index.php的自定義布局樣式。 我的主題稱為kendo,我正在嘗試在functions.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' );
    }

您為is_page_template掛鈎使用了錯誤的參數。 使用帶有擴展名的完整模板文件名。 index.php不是index

請參見函數is_page_template參考。

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

/*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中的兩個參考答案: 一個兩個

不會將其添加到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