- This topic is empty.
-
AuthorPosts
-
September 8, 2014 at 7:49 am #182135
Senff
Participantis_page_template(templateName)
IS the if statement I was referring to ;)The problem is that a template check can’t be called just everywhere you like — apparently it only returns something when it’s called in The Loop, so I’m trying to find another way to check for the template.
September 8, 2014 at 7:54 am #182136chrisburton
Participantis_page_template(templateName) IS the if statement I was referring to ;)
Sorry. Was directing my statement to the OP.
The problem is that a template check can’t be called just everywhere you like — apparently it only returns something when it’s called in The Loop, so I’m trying to find another way to check for the template.
This guy is doing it (even with the
if
statement) and it works. Hmm.http://wordpress.org/support/topic/conditional-wp_enqueue-with-is_page_template
Resources:
September 8, 2014 at 8:03 am #182138Senff
ParticipantI just found out that
is_page_template
only works if it’s an actual PAGE — it won’t work on POSTS.Instead, the check should be done on something like this:
if( 'mycustomtypename' == $post_type ) { wp_enqueue_script( 'googleapi' ); wp_enqueue_script( 'googlemaps' ); }
This guy is doing it (even with the if statement) and it works. Hmm.
I don’t think he actually said it works with that conditional statement…. I may be wrong. @taxicss can you elaborate please?
September 8, 2014 at 8:15 am #182140chrisburton
ParticipantI don’t think he actually said it works with that conditional statement
You’re right. It was another user on the WP forum that said it worked. Still kind of weird.
September 8, 2014 at 9:05 am #182152Alen
Participantadd_action( 'wp_enqueue_scripts', 'init_theme_scripts_styles' ); function init_theme_scripts_styles() { global $wp_styles; # scripts # following code echos jQuery wp_enqueue_script( 'jquery' ); # wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); # Following code loads master.min.js from the js folder in your theme # see: http://codex.wordpress.org/Function_Reference/wp_enqueue_script wp_enqueue_script( 'main', get_template_directory_uri() . '/js/master.min.js', ['jquery'], '1.0', true ); # this is a conditional to check if we are on certain page, then we echo our whatever script # you can do these conditionals for both, the CSS/JS within this code if ( is_page( 'ENTER-SLUG-HERE' ) ) { wp_enqueue_script( 'NAME', get_template_directory_uri() . '/js/NAME.js', ['jquery'], '', true ); } # styles # wp_enqueue_style( $handle, $src, $deps, $ver, $media ); # see: http://codex.wordpress.org/Function_Reference/wp_enqueue_style # following code loads master.min.css file from the css folder in your theme wp_enqueue_style( 'main', get_template_directory_uri() . '/css/master.min.css', [], '1.0', 'all' ); }
September 8, 2014 at 9:12 am #182153Alen
Participantin the code above if you’re using older version of php, you might need to replace,
['jquery']
witharray('jquery')
-Alen
September 8, 2014 at 9:27 am #182157Senff
ParticipantNo, that won’t work. As stated before, the script needs to be loaded when it’s a custom POST.
if ( is_page( 'ENTER-SLUG-HERE' ) ) {
will only work if it’s a PAGE. What might work is IS_SINGULAR.
@taxicss can you try this for fun?// add google maps function load_google_script() { wp_register_script( 'googleapi', "https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"); wp_register_script( 'googlemaps', get_template_directory_uri() . '/js/googlemaps.js', array(), '1.0.0', true ); if(is_singular('YOURPOSTTYPE')) { wp_enqueue_script( 'googleapi' ); wp_enqueue_script( 'googlemaps' ); } } add_action( 'wp_enqueue_scripts', 'load_google_script' );
Not tested but give it a try. If it doesn’t work I’ll have to check it at home later today.
September 8, 2014 at 10:05 am #182161taxicss
Participant@Senff Yes sir it’s working now with your last code. Thanks a lot. Now maybe I’ll try to make it more better by making it a plugin instead like you said.
Again, thanks to all @Senff, @chrisburton, @Alen
September 8, 2014 at 11:07 am #182179Alen
ParticipantOh sorry guys somehow I missed the part about custom posts. Need to stop multitasking.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.