Support » Themes and Templates » Using jQuery and WordPress

  • Hello all. I just began learning how to develop WordPress templates, and I have mostly finished all the features I wanted to include in my first template, except one.

    I wanted to have a jQuery powered image slide show on the main page– which is just a page template that is exactly like a regular index.php file.

    I have a bunch of divs set up for each image, and caption on in the page template, above the posts area.

    I then added the JavaScript in header.php. It for some reason does not work. Am I doing something wrong?

Viewing 2 replies - 1 through 2 (of 2 total)
  • I have recently ran into this problem myself, and would be interested in knowing if you find a solution.



    Instead of just adding the javascript (if you did it manually), you should use wp_enqueue_script to avoid clashing with plugins that also use jQuery. You’d just need to call wp_enqueue_script('jquery'); before the wp_head(); or before get_header();
    Or if you are using an external js file like you should, then you’d have to do something like wp_enqueue_script('my-script', '/wp-content/themes/my-theme/my-script.js', array('jquery'), '1.0');

    Make sure to note that WordPress loads jquery in noconflict mode, so you have to format your jQuery as follows:

    some code...

    if you want to use the $ function.

    If you find that your theme isn’t printing out the <script...>...</script> then you can use the functions.php to add_action('wp_footer', 'wp_print_scripts'); or just manually call wp_print_scripts(); somewhere in the theme where you want them to print. Be careful if doing something like this though, because you’ll probably want to also include something in the functions.php to remove_action('wp_head', 'wp_print_head_scripts', 9);
    remove_action('wp_footer', 'wp_print_footer_scripts');. To avoid printing jQuery twice if you enqueue it after the wp_head();

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using jQuery and WordPress’ is closed to new replies.