Home > Software design >  jquery is not working in wordpress and throws an error [duplicate]
jquery is not working in wordpress and throws an error [duplicate]

Time:10-07

I'm trying to run this jquery code on my archive page, but it gives me an error "undefined $". But it works outside of wordpress. I don't know what to do, I'm confused.

$(".show-more-posts").on("click", () => {
    $(this).show();
 });

Sorry if my question is stupid, I'm a novice.

CodePudding user response:

You would need to load jquery in wordpress. Use this code in functions.php file.

add_action('wp_enqueue_scripts', 'load_jquery_to_theme');

function load_jquery_to_theme(){
  wp_enqueue_script('jquery');
}

And in your javascript file use this

jQuery(document).ready(($) => {

  $(".show-more-posts").on("click", () => {
    $(this).show();
  });

});
  • Related