functions.php –one function with conditional tags isn’t working
-
Hope someone can help me out.
I had originally loaded a bunch of scripts for a site I’m working on in the header.php file, using conditional tags so that they only loaded on pages in which they were needed. In an attempt to use better practices, I’ve just moved everything over to the functions.php file. The problem is that all of the Conditional Tags don’t seem to be working correctly (if(is_home)){};, if(is_page()){};, etc.). They worked perfectly while in header.php.
If I comment out all of the conditional tags, everything works perfectly. I also have several other functions in the file that use conditional tags, and those work well.
I’ve also tried adding
global $post;to the function, and that didn’t make a difference. (I can’t say I fully grasp when it is required yet).Here’s what I have:
function my_scripts_init() { $scriptinitDir = get_bloginfo('template_directory').'/js/'; if(!is_admin()) { wp_register_script('my_sidebar_script', $scriptinitDir.'sidebar-nav.js', false); wp_enqueue_script('my_sidebar_script'); wp_deregister_script('jquery'); wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2'); wp_enqueue_script('jquery'); wp_enqueue_script('toggle_val', $scriptinitDir.'jquery.toggleval.js', array('jquery')); //default search text in header search bar // swfObject for flash header on home page if (is_home()) { wp_enqueue_script('my_swf_object', $scriptinitDir.'swfobject.js', false); } // Different inits for Accordion Scripts if (is_page('54') || $post->post_parent == '54') { //portfolio sidebar init wp_enqueue_script('sidebar_nav_portfolio', $scriptinitDir.'portfolio.js', array('my_sidebar_script')); } elseif (is_page('2') || $post->post_parent == '2') { // rest of About sidebar init wp_enqueue_script('sidebar_nav_about', $scriptinitDir.'about.js', array('my_sidebar_script')); } else { //All other sidebar init wp_enqueue_script('sidebar_nav_init', $scriptinitDir.'sidebar-nav-init1.js', array('my_sidebar_script')); } // Zebra lists fpr the News & Press page if ( is_page('45') || $post->post_parent == '2') { wp_enqueue_script('my_zebra_stripes', $scriptinitDir.'jquery.alternate.js', array('jquery')); } } } add_action('init', 'my_scripts_init');
The topic ‘functions.php –one function with conditional tags isn’t working’ is closed to new replies.