admin管理员组

文章数量:1379611

I try to pass the post/pages ID to my JavaScript file. url and template_url works but the id just return 0 on every post.

PHP:

// Register scripts
wp_enqueue_script('scripts', get_stylesheet_directory_uri() . '/js/scripts.js', array(), '1.0', true);

// Pass PHP data
wp_localize_script('scripts', 'phpdata', array(
    'template_url' => get_bloginfo('template_url'),
    'url' => get_bloginfo('url'),
    'id' => get_queried_object_id()
));

JavaScript:

console.log('ID' + phpdata.id);

本文标签: wp queryPass post ID to JavaScript outside the loop