Custom Template with Header Code

Community Forums Forums General Discussion Custom Template with Header Code

This topic is: resolved

This topic contains 7 replies, has 2 voices, and was last updated by  polozone 1 year, 2 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #43421

    polozone
    Participant
    Post count: 4

    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
    */
    ?>
    <?php

    do_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();

    #43480

    wpspeak
    Participant
    Post count: 112

    That’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.


    #43854

    polozone
    Participant
    Post count: 4

    thank 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!

    #43880

    polozone
    Participant
    Post count: 4

    This 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’);
    }

    #43902

    polozone
    Participant
    Post count: 4

    this 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’);
    }
    }

    #44023

    wpspeak
    Participant
    Post count: 112

    Sorry just noticed your replied. For some reason the email forwarding system does not work.

     

    Glad to hear you figured it out!


    #44025

    wpspeak
    Participant
    Post count: 112

    Will 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.


    #44164

    polozone
    Participant
    Post count: 4

    I marked it resolved, thanks so much for your help on this issue!

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.