【问题标题】:Wordpress - loading custom css on index.phpWordpress - 在 index.php 上加载自定义 CSS
【发布时间】:2015-08-12 00:11:27
【问题描述】:

我正在尝试为 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' );
    }

【问题讨论】:

  • 请详细说明您要达到的目标。

标签: php css wordpress layout


【解决方案1】:

您为is_page_template 挂钩使用了错误的参数。使用带扩展名的完整模板文件名。 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

【讨论】:

  • 谢谢,我用 index 和 index.php 参数都试过了,但还是不行。我已经通过使用不同的 css 类解决了这个问题,这可能并不理想,但它可以工作......
【解决方案2】:

将它添加到 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 } ?>

【讨论】:

  • 谢谢,我也试试这个方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-04
  • 1970-01-01
相关资源
最近更新 更多