Community Forums › Forums › Archived Forums › General Discussion › Custom Template with Header Code
- This topic has 7 replies, 2 voices, and was last updated 10 years, 10 months ago by polozone.
-
AuthorPosts
-
May 31, 2013 at 4:57 pm #43421polozoneMember
I'm trying to make a template that has custom javascript that I can use for just some pages, not all. I have a lot of pages that use different kinds of javascript.
I have this, but it ends up repeating the head twice. What should I do to insert javascript into the head, and then continue on with the rest of the genesis loop?
<?php
/*
Template Name: CookieCode-in-Head
*/
?>
<?phpdo_action( 'genesis_doctype' );
do_action( 'genesis_title' );
do_action( 'genesis_meta' );/*wp_head(); /** we need this for plugins **/
?><SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}// End -->
</script>
</head>
<body <?php body_class(); ?>>
<?php
do_action( 'genesis_before' );
?>
<?php
genesis();June 1, 2013 at 10:57 am #43480wpspeakMemberThat's not a right way to reg a js in WordPress.
You do not need to dev a custom template for each single page.
You can read this great post on enqeueu js in WordPress http://wpcandy.com/teaches/how-to-load-scripts-in-wordpress-themes/#.UaonJUDVCSo
With the help of WordPress conditional tags, you can specify where the js will be used, otherwise deregister.
June 3, 2013 at 10:56 am #43854polozoneMemberthank you I'll look in to that. Will I be able to direct, say many different scripts to specific sets of pages with this method? I've got different sets of forms with different validations and other types of things. I appreciate your help!
June 3, 2013 at 12:21 pm #43880polozoneMemberThis looks great - I see this content (below) - can the "if (is_front_page() )" be adapted to look for a template? what would that syntax be?
"if (is_templateName() )" would that work? will try it
if ( is_front_page() ) {
wp_enqueue_script('home-page-main-flex-slider');
}June 3, 2013 at 1:31 pm #43902polozoneMemberthis is working now, thanks for the great information - such a better way to do it!
in case it would help someone, here it is for child theme, and more than one page template
add_action( 'wp_enqueue_scripts', 'wpk_load_javascript_files' );
/*Register some javascript files, because we love javascript files. Enqueue a couple as well*/
function wpk_load_javascript_files() {
wp_register_script( 'KBcookie', get_stylesheet_directory_uri().'/js/cookie.js', array(), '1.0', true );
if ( is_page_template('cookie-head-template-no-header.php') || is_page_template('cookie-head-template.php') ) {
wp_enqueue_script('KBcookie');
}
}June 4, 2013 at 9:41 am #44023wpspeakMemberSorry just noticed your replied. For some reason the email forwarding system does not work.
Glad to hear you figured it out!
June 4, 2013 at 9:42 am #44025wpspeakMemberWill you be able to mark this thread as resolved? I'm not a mod, so I don't have the permission to do so. As someone who opens the thread, you probably can mark it as resolved.
June 5, 2013 at 10:19 am #44164polozoneMemberI marked it resolved, thanks so much for your help on this issue!
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.