【问题标题】:Wordpress Multiple stylesheetsWordpress 多个样式表
【发布时间】:2017-01-09 08:03:25
【问题描述】:

我正在尝试在我的网站上添加多种样式。对我来说,这段代码应该可以工作,但如果有人能告诉我为什么它不是那么好!

if (is_front_page()) {
        wp_enqueue_style('custom-frontpage', get_template_directory_uri().'/stylee.css');
} else{
        wp_enqueue_style('custom-frontpage', get_template_directory_uri().'/css/temp.css');
}

这位于我的functions.php

【问题讨论】:

  • 这位于我的functions.php中

标签: php css wordpress


【解决方案1】:

is_front_page()functions.php 中使用时总是返回 false 。您必须使用wp 创建一个钩子才能使其正常工作。不工作的原因是因为functions.php启动时Wordpress不知道查询的内容,所以它不会知道你是页面的类型。试试这个:

add_action( 'wp', 'check_is_front_page' );
function check_is_front_page() {
   if(is_front_page()){
      wp_enqueue_style('custom-frontpage', get_template_directory_uri().'/stylee.css');
  }else{
      wp_enqueue_style('custom-frontpage', get_template_directory_uri().'/css/temp.css');
  }
}

您可以阅读有关此特定钩子的信息here

您可以阅读有关钩子及其工作原理的更多信息here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    相关资源
    最近更新 更多