In the footer i load the Google maps api. But only on the contact page, i used the google maps api. Now i want to make code. That the google maps api is only load in the contact template pages. How can i make that.
This is the script tag in the footer.
<script type="text/javascript" src="//maps.google.com/maps/api/js?v=3&sensor=false"></script>
That script tag. Must only load in the template contact page.
Thank for helping
First a bit of advise (since the solution is based on it) – always “enqueue” your scripts, don’t just add them in the footer. Read this, for example .
Now the solution for loading scripts on specific template, since this is what you asked for:
EDIT: add this to your functions.php
You’ve got »Conditional Tags« in WordPress. Those allow you do determine if some condition meets or not (basically those are parts of the
$wp_query
object, just wrapped with a public API function).In detail: There’s
is_page()
, which tells you if you’re on the desired page not.So just wrap it into a function, hook in at the right hook and abort if you’re not on the desired page.
The following goes into your functions.php file (or a small custom plugin).
Javascript (and css) should be added with
wp_enqueue_script
.Register the script but don’t enqueue it yet in
functions.php
.Call the enqueue on the contact template page before
wp_head()
,which should be located in
header.php
so beforeget_header()
.registering
andqueueing
is confusing at first here is a good tutorial