【问题标题】:WordPress custom page CSSWordPress 自定义页面 CSS
【发布时间】:2019-04-07 00:49:35
【问题描述】:

我尝试做一些自我研究,发现了这些问题12,但没有一个真正解决了我的问题,所以我创建了一个新问题。

我尝试使用wp_enqueue_scripts 函数将特定的 CSS 文件加载到特定的页面,但不知何故,它总是跳过条件并进入 else 条件。这是我的代码;

function theme_script_enqueue() {
    if(is_page_template('index.php')) {
        wp_enqueue_style('indexstyle', get_template_directory_uri() . '/css/news.css', array(), '1.0.0', 'all');
        wp_enqueue_script('indexjs', get_template_directory_uri() . '/js/main.js', array(), '1.0.0', true);
    } else {
        wp_enqueue_style('mainstyle', get_template_directory_uri() . '/css/main.css', array(), '1.0.0', 'all');
        wp_enqueue_script('mainjs', get_template_directory_uri() . '/js/main.js', array(), '1.0.0', true);
    }
}

add_action('wp_enqueue_scripts', 'theme_script_enqueue');

当我访问http://localhost:8888/wordpress/index.php 只是为了确保根据条件,我希望脚本加载news.css,但它加载了main.css,它们有一些颜色不同的元素,所以我立即发现了。我确实在条件之间进行了切换 CSS 文件,当我尝试访问 index.php 时它总是加载 else 条件。我是不是做错了什么?

【问题讨论】:

    标签: php css wordpress


    【解决方案1】:

    如此处所述:https://developer.wordpress.org/themes/template-files-section/page-template-files/

    index.php — 如果没有指定或找到特定的页面模板,WordPress 默认使用主题的索引文件来呈现页面。

    所以据我了解,index.php 是一个后备而不是模板。 您还必须描述您的模板文件:

    <?php /* Template Name: Example Template */ ?>
    

    您是否将该评论写入您的模板文件?

    【讨论】:

    • 所以我尝试从is_page_template 更改为is_page,目标仍然是index.php,但脚本仍然没有使用news.css 到我的index.php。我有什么办法可以将特定的 css 文件加载到 index.php 并将它们与其他 css 文件分开?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2020-08-06
    相关资源
    最近更新 更多