J
J
JyriG2018-07-29 09:33:11
JavaScript
JyriG, 2018-07-29 09:33:11

When adding a page to WordPress, js stopped working. How to fix?

I decided to connect scripts and styles through the functions.php file.
Styles are loaded, but js is not.
Here is the code itself in functions.php:

<?php

/**
* загружаемые скрипты и стили
*/
function load_style_script(){
  wp_enqueue_script('main', get_template_directory_uri() . '/js/main.js');
  wp_enqueue_script('slider', get_template_directory_uri() . '/js/slider.js');
  wp_enqueue_style('style', get_template_directory_uri() . '/style.css');
  wp_enqueue_style('normalize', get_template_directory_uri() . '/css/normalize.css');
  wp_enqueue_style('media', get_template_directory_uri() . '/css/media.css');
}

/**
* загружаем скрипты и стили
*/
add_action('wp_enqueue_scripts', 'load_style_script');
¨
How to make js work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan Hasanli, 2018-07-29
@azerphoenix

Hello!
1) if these are jquery scripts, then be sure to follow the connection order (add jquery dependencies).
2) Scripts and styles are strongly recommended to connect via functions.php
3) Look in the console. Most likely the scripts are connected, but there are errors in the scripts. The reason - it is necessary to replace the $ signs with jQuery
In the initialization scripts. Most likely it is your main.js that you need to wrap the js code (so as not to replace each $ character)

jQuery( document ).ready(function( $ ) {

......

});

I'm not sure, but try also loading the main.js script after slider.js
Try also adding wp_register_script() to the connection code
Simple example:
// подключаем стили и скрипты
function register_styles_scripts() {
//стили
  wp_register_style('bootstrapCSS', get_template_directory_uri() .
    '/css/bootstrap.min.css');
  wp_enqueue_style('bootstrapCSS');
//скрипты
  wp_deregister_script('jquery');
  wp_register_script('jquery', get_template_directory_uri() .
    '/js/jquery-3.2.1.min.js');
  wp_enqueue_script('jquery');
  
  wp_register_script('PopperJS', get_template_directory_uri() .
    '/js/popper.min.js');
  wp_enqueue_script('PopperJS');
// шрифты
  wp_enqueue_style('google-fonts-Lato', 'https://fonts.googleapis.com/css?family=Lato:300,400,400i,700', array(), null, 'all' );
  wp_enqueue_style('google-fonts-Niconne', 'https://fonts.googleapis.com/css?family=Niconne', array(), null, 'all' );
}
add_action('wp_enqueue_scripts', 'register_styles_scripts');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question