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.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)