• Resolved Anonymous User 15521305

    (@anonymized-15521305)


    Hello,

    How can I load JS Script for specific custom post type? I try the code below but it’s not working:

    add_action( 'wp_enqueue_scripts', 'load_custom_js' ); 
    function load_custom_js(){
       if( is_singular() && get_post_type()=='location' ){
    	wp_enqueue_script( 'google-map', get_stylesheet_directory_uri() . '/maps.js',  array('jquery'), '3.6.0', true );
      }
     
    }
    add_action('wp_enqueue_scripts', 'load_custom_js');

    Not sure if this is related to the support.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter Anonymous User 15521305

    (@anonymized-15521305)

    For more information, I want to load different Javascripts for different post types (Location and Post). Let’s say load maps.js for Location and maps2.js for post. But using the one below together with the above is not working. It doesn’t work for both at same time.

    wp_enqueue_script( 'google-map2', get_stylesheet_directory_uri() . '/maps2.js', array('jquery'), '3.6.0', true );

    Thread Starter Anonymous User 15521305

    (@anonymized-15521305)

    sorry, soveld.

    add_action( 'wp_enqueue_scripts', 'load_custom_js' ); 
    function load_custom_js(){
       if( is_singular() && get_post_type()=='location' ){
    	wp_enqueue_script( 'google-map', get_stylesheet_directory_uri() . '/maps2.js',  array('jquery'), '3.6.0', true );
      }
      elseif( is_singular() && get_post_type()=='post' ){
    	wp_enqueue_script( 'google-map2', get_stylesheet_directory_uri() . '/maps.js',  array('jquery'), '3.8.0', true );
      }
    }
    add_action('wp_enqueue_scripts', 'load_custom_js');
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Good to hear you got this solved. Not quite sure why the logic wasn’t working originally as it all looks sound to me.

    Thread Starter Anonymous User 15521305

    (@anonymized-15521305)

    Actually it works but later I’ve just relaized that I misunderstand about what I tried to do.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Gotcha 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Load JS Script for spesific custom post type’ is closed to new replies.