dongwuzun4630 2017-03-01 02:09
浏览 66

为什么我的WordPress模板不能识别我的JQuery库和CSS文件?

I created the following scripts and added them the functions.php file within my WordPress them:

add_action('wp_enqueue_scripts', 'scripts_styles');
function scripts_styles(){
     if(is_page_template('Disclosures.php')){
       wp_enqueue_script('jquery', get_template_directory_uri().'/js/jquery.validate.js', array(), GOVPRESS_VERSION, false );
       wp_enqueue_script('jquery', get_template_directory_uri().'/js/jquery.min.js', array(), GOVPRESS_VERSION, false );
       wp_enqueue_script('bootstrap', get_template_directory_uri().'/js/bootstrap.min.js', array(), GOVPRESS_VERSION, false );
       wp_enqueue_script('handlebars',  get_template_directory_uri().'/js/handlebars.min.js', array(), GOVPRESS_VERSION, false );
       wp_enqueue_script('addrows', get_template_directory_uri().'/js/addrows.js', array(), GOVPRESS_VERSION, false );
   }
}

function wpse_enqueue_page_template_styles() {
    if ( is_page_template( 'Disclosures.php' ) ) {
        wp_enqueue_style( 'bootstrapthemecss', get_template_directory_uri() . '/css/bootstrap-theme.min.css', array(), GOVPRESS_VERSION, false );
        wp_enqueue_style( 'bootstrapcss', get_template_directory_uri() . '/css/bootstrap.min.css', array(), GOVPRESS_VERSION, false );
        wp_enqueue_style( 'maincss', get_template_directory_uri() . '/css/main.css', array(), GOVPRESS_VERSION, false );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse_enqueue_page_template_styles' );

However, they are not recognized by my custom template.

In other words, I tested my page with these scripts and stylesheets and they work very well.

I was able to validate form fields and display the results in the styles that was specified.

Yet, they are not validating the same form fields and page is not being styled.

What am I missing?

Just an FYI, the name of the theme is govpress.

Thanks for your help in advance.

  • 写回答

1条回答 默认 最新

  • dsc7188 2017-03-01 03:03
    关注
    1. You seem to be enqueuing two scripts with the same name: jquery. However, jquery is already present in WordPress and you might notice wp_enqueue_script() description says: does NOT override. This means whenever you are trying to enqueue a script with the same name as a previously enqueued script... nothing happens.

    2. If you're not happy with the existing version of jquery embedded in WP, you need to deregister it and register the new one, with this exact name: jquery. The name is very important, as a lot of WP scripts depend on jquery. If you replace it, they'll depend on your version instead of the included one. Make sure you enqueue a version of jquery regardless of your templates logic (if you removed the default one).

    3. You should specify jquery as a dependency in the dependencies array of each script that depends on jquery.

    4. If none of your scripts and styles load, you might want to debug your script checking if is_page_template('Disclosures.php') returns true in the page you're testing this on. A simple die('this just happened...') will do for testing purposes.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站