• Resolved Oxygen89

    (@oxygen89)


    Hi all,

    If i need to put any js/jqery for a specific page, then , where should i put that. For eg. to put the css, we use child theme style.css. So, for js is there any file?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Not my strong point, but I think this should work (Add to Child Theme functions.php):

    //ADD THE SCRIPT PROVIDED AFTER THE BODY TAG
    add_action('__before_header' , 'my_js_script');
    function my_js_script() {
    
        global $wp_query;
        //we get the current object id
        $id = $wp_query->get_queried_object_id();
    
        //we check if we display a page AND if the page has the wanted id
        if (is_page() && YOURPAGEID == $id) {
            ?>
                <script type='text/javascript'>
    
    ADD SCRIPT HERE
    
            </script>
            <?php
        }
    }

    I think it should be much easier if you know the page id. You shouldn’t need all the wp_query stuff, Dave. It also depends on what the script is, because this will tell you whether you need to load it in the header or the footer. If it’s something like a facebook fancy-like-float thing—that is, not essential to the page loading—then it should be loaded in the footer, so as not to slow the page down. If it is fundamental to getting the content of the page then it will need to be in the header. Most scripts can happily be loaded in the footer, so the page is delivered quickly to the user and then the script is loaded in the microseconds during which the user’s brain is adjusting to the fact that the page arrived in their browser.

    The code would be something like this:

    add_action('wp_footer', 'add_my_extra_js', 100);
    function add_my_extra_js(){
        if ( is_page(42) ) {
            ?>
            <script type="text/javascript">
                //YOUR SCRIPT HERE
            </script>
            <?php
        }
    }

    If the script is an entire file, then the process is different.

    You add this to your functions.php file in your child theme. If you’ve never done this, read this article on customizing Customizr first.

    Thread Starter Oxygen89

    (@oxygen89)

    @electric feet: I need to include the script in the home page as well as the script will be above the featured images to display some animation. So, that is my requirement.

    So, add_action(‘wp_footer‘ part will be what?

    Thanks

    Add it to the footer, otherwise the loading of your whole page will be held up while the animation loads.

    If you get problems, switch to the header, but you probably won’t have problems.

    Thread Starter Oxygen89

    (@oxygen89)

    @electricfeet: Thanks a lot.:)

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Where to put Custom js/jquery’ is closed to new replies.