WordPress.org

Ready to get started?Download WordPress

Forums

Using jQuery and WordPress (3 posts)

  1. Aaron S.
    Member
    Posted 5 years ago #

    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?

  2. akaney
    Member
    Posted 5 years ago #

    I have recently ran into this problem myself, and would be interested in knowing if you find a solution.

  3. ty
    Member
    Posted 5 years ago #

    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:

    jQuery(function($){
    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);
    and
    remove_action('wp_footer', 'wp_print_footer_scripts');. To avoid printing jQuery twice if you enqueue it after the wp_head();
    peace~

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.