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

标签 php css wordpress layout

我正在尝试加载 index.php 的自定义布局样式。我的主题叫做剑道,我正在尝试在 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 Hook 使用了错误的参数。使用带扩展名的完整模板文件名。 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

关于php - Wordpress - 在 index.php 上加载自定义 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524290/

相关文章:

php - 执行从 AJAX 响应返回的 javascript 函数 (PHP)

html - 将元素定位在换行文本的右上角

html - 如何使图像和文本水平居中的标题?

XML 解析错误 : XML or text declaration not at start of entity in wordpress

html - 即使在使用显示 :inline-block 后,社交媒体图标也不会出现在一行中

php - 没有错误,但数据未使用 PDO 插入数据库

php - 建立数据库连接时出错

php - 如何在 laravel 4.2 中获取可填充变量作为数组

jquery - 使用简单表单自定义 CSS

php - Wordpress 子主题 css 崩溃