Answer the question
In order to leave comments, you need to log in
Wordpress how to include certain js on one page?
Good afternoon everyone. Tell me how in wordpress to connect certain JS on one page, others on another. Head closes in header.php, and body opens in the same place.
those. you need to connect in the body itself, is it real?
And if not, what are the other ways?
Answer the question
In order to leave comments, you need to log in
WordPress has great conditional tags, like is_page() , where you can pass the page/post ID in the parameters and get the result accordingly:
if (is_page(id)){
wp_enqueue_script('needFullScript'); // In the body of the function, we connect the desired script, before that we register it using wp_register_script()
}
is_page - for pages
is_single - for writing
////////////////
function my_styles() {
if ( is_page( 933 ) ) {
//подключаем стиль
wp_enqueue_style ( 'contact', get_template_directory_uri()
. '/altercss.css', array(), '1.0' );
//подключаем скрипт
wp_enqueue_script('alterscript', get_template_directory_uri() . '/alterscript.js');
}
}
add_action( 'wp_enqueue_scripts', 'my_styles' );
//////////////////
Now, of course, I’ll write nonsense, but since no one writes - as they say in fishlessness and a diver - a snack.
I see 2 options:
the first - through some if ... (I hope you understand what I mean)
the second - if you don’t have 100500 pages there - connect everything (and even though everything should work, I give a 100% guarantee that this option is not right)
You can create your own page template and write the plug-in JS in the head at the top, you can read how to do this here or here
There is no universal answer. If you have something simple use JS in body:
https://learn.javascript.ru/external-script
I found a solution like this:
1. Add a hook to init.php
add_action( 'wp_enqueue_scripts', 'alps_gutenberg_blocks_scripts' );
function alps_gutenberg_blocks_scripts(){
wp_enqueue_script( 'alps_gutenberg_front', plugins_url('src/front.js', plugin.php'), array( 'jquery' ) );
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question