如果将本地化脚本函数挂接到wp_enqueue_scripts
, 然后您将可以访问global $post
变量只要您在“wp”时或之后选择挂钩,您就应该可以访问global $post
.
<?php
add_action(\'wp_enqueue_scripts\', \'YOUR_NAME_scripts\');
function YOUR_NAME_scripts() {
wp_enqueue_script(\'YOUR_NAME-js\');
global $post;
$params = array(
\'site_url\' => site_url(),
\'admin_ajax_url\' => admin_url(\'admin-ajax.php\'),
\'post_id\' => $post->ID
);
wp_localize_script( \'jquery\', \'YOUR_NAME\', $params );
}