您可以使用wp_localize_script();
:
// Example:
wp_register_script( \'your-template-script\', $path, $dependencies, $version, true );
wp_enqueue_script( \'your-template-script\' );
wp_localize_script(
\'your-template-script\'
,\'your_template_object\'
,array(
\'ajaxurl\' => admin_url( \'admin-ajax.php\' )
,\'template_nonce\' => wp_create_nonce( self :: $search_nonce_val )
,\'action\' => \'build_template\'
,\'title\' => get_the_title()
,\'content\' => get_the_content()
,\'title_format\' => \'h1\'
)
);
然后可以在模板中使用它。js文件如下:
function onAJAXSuccess( target_id, obj ) { // calling this on ajax.success
var
title = obj.title
content = obj.content
string = \'<\'+obj.title_format+\'>\'+title+\'</\'+obj.title_format+\'>\'+content;
;
jQuery( \'#\' + target_id ).html( string ).fadeIn();
}
然后这样称呼它:
onAJAXSuccess( \'template_container_id\', your_template_object );
.