开发者

Problem with is_page() conditional in WordPress

开发者 https://www.devze.com 2023-01-06 21:59 出处:网络
can someone tell me why th开发者_开发问答is code doesn\'t work? The is_page() is not in effect.

can someone tell me why th开发者_开发问答is code doesn't work? The is_page() is not in effect.

function tao_scripts() {
if (!is_admin()) {


    wp_deregister_script('jquery');
    wp_register_script('jquery', get_template_directory_uri() . '/js/jquery-1.4.2.min.js', false, '1.4.2');
    wp_enqueue_script('jquery');

    if ( is_page() ) {
        wp_register_script('jquery-validate', get_template_directory_uri() . '/js/jquery.validate.min.js', 'jquery', '1.7');
        wp_enqueue_script('jquery-validate');
    }       
  }
}
add_action('init', 'tao_scripts');

The code is in a php file which is included i my functions.php.

I have tried all I could find/think of. Without the is_page() conditional it works. I have tried with wp_reset_query(), no help. Im sure wordpress knows its a page. is_page(34, 'name', 'Name') wont help either.


It is to do with the order of actions

add_action('init', 'tao_scripts');

this action occurs way before the query object is extracted


Technically, Torok is wrong. is_page works perfectly fine outside the loop. I imagine it's not working because you're not using wp_register_script correctly. The dependencies need to be an array:

wp_register_script('jquery-validate', get_template_directory_uri() . '/js/jquery.validate.min.js', array('jquery'), '1.7');

That should do it.

EDIT:

As TheDeadMedic pointed out, you should also hook this on a later hook. The earliest one you should use is wp. I'd actually suggest using template_redirect, since that will only run if you're on the front end (unless you want to replace jQuery on the back end too).


Can you try to test your function with specific page:

if ( is_page(34, 'name', 'Name') ) 
{
    // your code here
}
0

精彩评论

暂无评论...
验证码 换一张
取 消