Support » Developing with WordPress » Not sure if my if( is_page(18) ) { is in the exact correct place

  • Hi, I am not sure where to exactly put my if( is_page(18) hook when I enqueue my script.
    And I am confused about the action hook as well. Do I need to make another seperate action hook and say where exactly on the page I want my code to work.
    Here is the full code:

    <?php
    
    function childtheme_parent_styles() {
     wp_enqueue_style( 'parent', get_template_directory_uri().'css/style.css' );
    function mytheme_files() {
     wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
    }
    
    add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');
    
    wp_register_script('main-js',get_stylesheet_directory_url().'/js/main.js');
    
    }
    
    add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
    }
    
    function pr_scripts_styles() {
    wp_enqueue_script( 'main.js');      
    if( is_page(18) ) {
    
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • zworthkey12vaibhav

    (@zworthkey12vaibhav)

    Hi @jillyspence,

    Your code is correct. Just enqueue script inside if loop for page id 18.

    If you face any trouble let me know.

    Thank You

    Thread Starter jillyspence

    (@jillyspence)

    Hi, how would I do that, as I have researched a lot about how to enqueue the if statement, but cannot seem to find out exactly how to enqueue the if statement.
    I have taken the if statement an put it before wp_enqueue_script now as follows:
    Is this what you mean. If you can help me here again please.

    <?php
    
    function childtheme_parent_styles() {
     wp_enqueue_style( 'parent', get_template_directory_uri().'css/style.css' );
    function mytheme_files() {
     wp_enqueue_style('mytheme_main_style', get_stylesheet_uri());
    }
    
    add_action( 'wp_enqueue_scripts', 'childtheme_parent_styles');
    
    wp_register_script('main-js',get_stylesheet_directory_url().'/js/main.js');
    
    }
    
    add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
    }
    
    function pr_scripts_styles() {
    if( is_page(18) ) {
    wp_enqueue_script( 'main.js');      
    
    }

    Hi @jillyspence,

    Everything is fine just use the name of the js file you registered above.

    for reference

    add_action( 'wp_enqueue_scripts', 'pr_scripts_styles' );
    }
    
    function pr_scripts_styles() {
    if( is_page(18) ) {
    wp_enqueue_script( 'main-js');      
    
    }

    It will work

    Thank You

    Thread Starter jillyspence

    (@jillyspence)

    It still doesn’t work.
    I have Elementor in wordpress, and put the html in Elementor, as if I want to put the html in wordpress, I would have to take Elementor builder out, as if I click on back to wordpress button to edit, a warning comes up, that I could break the site.
    Do, you think it is not working, because of Elementor, or should it work in there if it shows the html and css part working. ( showing the clock face). Just not ticking or doing anything.
    Or do you think, its because I still have wordpress 5.7.2 installed locally on my computer. Or do you think it could be the php version 7.3.5 I have with wordpress.

    zworthkey12vaibhav

    (@zworthkey12vaibhav)

    @jillyspence That should be working.
    You can check if the script is enqueued using this function on that page using of any of the hook wp_script_iswp_script_is()`.

    Determine if that script is enqueued.

    I will check your function in my environment and give the whole function.

    Don’t worry

    zworthkey12vaibhav

    (@zworthkey12vaibhav)

    @jillyspence I checked and rectified the code
    Paste this code

    function zwk_load_js() {
        if(is_page(18)){
        wp_register_script( 'gdgt-base', get_template_directory_uri() . '/js/main.js', array( 'jquery' ), NULL, false );
        wp_enqueue_script( 'gdgt-base' );
    }
    }
    
    add_action( 'wp_enqueue_scripts', 'zwk_load_js' );

    It works for me
    Check if that works for you, just change the file name.

    Hope that work

    Thank You

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.