【问题标题】:Enqueuing style and script based on template not working基于模板的排队样式和脚本不起作用
【发布时间】:2020-09-30 19:33:04
【问题描述】:

在 WordPress 中,我有一个名为 vertical-page.php 的模板文件。

当一个页面应用了这个模板时,我想加载一个特定的样式表和脚本。

我曾尝试使用is_page_template(),但它不起作用。查看页面源码时,没有加载脚本和样式?

<?php 

add_action('wp_enqueue_scripts', 'theme_scripts');

function theme_scripts() {
    wp_enqueue_style('bootstrap-grid-style', get_template_directory_uri() . '/assets/css/bootstrap-grid.css', array(), STYLE_VERSION);

    if ( is_page_template( 'vertical-page' ) ) {
      wp_enqueue_style('tmp-override', get_template_directory_uri() . '/assets/css/override.css', array(), STYLE_VERSION);
      wp_enqueue_script('overrides-script', get_template_directory_uri() . '/assets/js/main.js', array('jquery'),STYLE_VERSION, true);
    }
    
 ?>

【问题讨论】:

    标签: php wordpress php-enqueue


    【解决方案1】:

    试着把 add_action 放在函数后面

    【讨论】:

      【解决方案2】:

      如果您的页面模板文件是something.php,并且它的位置是活动主题的主文件夹,则需要将参数传递给is_page_template() 函数,如下所示:is_page_template('something.php')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-15
        • 1970-01-01
        • 2021-06-27
        • 2014-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多