0

I made this script in jquery

http://jsfiddle.net/eo3s9f7u/

I want to implement it in this site

http://avocat.dac-proiect.ro/wp/

I use Wordpress and my div is in footer.php

This is footer.php

    <?php

     ?>


                </div><!-- #main -->


        <footer id="colophon" class="site-footer" role="contentinfo">

                             <div id="top"></div>
                             <div id="mid"></div>
            <?php get_sidebar( 'footer' ); ?>

            <div class="site-info">
                <?php do_action( 'twentyfourteen_credits' ); ?>
                Codoban.com.All rights reserved
            </div><!-- .site-info -->
        </footer><!-- #colophon -->
    </div><!-- #page -->

    <?php wp_footer(); ?>
</body>
</html>

How can I implement it in this file so that it is functional?

Thanks in advance!

3 Answers 3

2

Others have suggested to include the JS code directly in the footer.php. I believe this is not the best approach.

Create, for example, a file js/main.js in the theme folder and use the wp_enqueue_script function with the wp_enqueue_scripts hook to enqueue your js file.

You can add the following (adjusted to your case) in the functions.php file in the theme folder:

function my_scripts_method() { wp_enqueue_script( 'themeMain', get_template_directory_uri() . '/js/main.js', array('jquery'), null, true ); }

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

Notice that the last parameter is set to true. This way your script will be loaded in the footer.

Also, jQuery scripts should be wrapped in jQuery(function($) {}); or similar in order to work with WordPress' noconflict. http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers

edit: Added the wp_enqueue_scripts hook and js wrapper by suggestion of Kaloyan Ivanov.

Sign up to request clarification or add additional context in comments.

8 Comments

As it says in the wordpress documentation: Name used as a handle for the script. codex.wordpress.org/Function_Reference/wp_enqueue_script. For example, if you had main2.js which depend on the main.js, you would do wp_enqueue_script( 'main2', get_template_directory_uri() . '/js/main2.js', array('jquery', 'themeMain'), null, true );
I added foloder /js/script.js...i added this code in functions.php wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array('jquery'), null, true )......and do not work...what is wrong?
Make sure that your script is actually being loaded by inspection get the page source I your browser. Close to the bottom you should see a script tag that includes your script.js. Make sure that all directories are correct. Read the WordPress documentation to make sure you understand well all the parameters.
@VRPF you should probably update your answer with the the wp_enqueue_scripts hook and the fact custom jQuery scripts should be wrapped in jQuery(function($) {}); or similar in order to work with WordPress' noconflict. Other than that, you have my upvote.
That is a good point. The WP documentation is clear in this regard, but it will make the answer clearer. I will add it.
|
0

Just add this before end of body

<script>
( "#top" ).click(function() {
  $( "#mid" ).slideDown( "slow", function() {
    // Animation complete.
  });

});
</script>

1 Comment

I did as you said but does not work ... nothing happens
0

Simply add the script anywhere in your footer.php file. Remember that your browser will read the script before reading any code which comes after it. For example, you can add it at the beginning:

    $( "#top" ).click(function() {
  $( "#mid" ).slideDown( "slow", function() {
    // Animation complete.
  });

});

</div><!--main -->

1 Comment

I did as you said but does not work ... nothing happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.