经过一些研究,感谢@Milo指出在wp
设置好了,我终于开始工作了。事实上,我必须尽快打电话wp
查询并生成了一些帖子/页面数据;
这是我的最终解决方案add_action( \'template_redirect\', \'nb_setup_globals\' );
以下是最终代码:
/**
* Setup globals
*/
global $nb_id, $nb_view, $nb_page;
function nb_setup_globals(){
global $post, $nb_id, $nb_view, $nb_page;
// Set default vars for globals
$nb_id = $post->ID;
$nb_view = \'front\';
$nb_post_name = \'index\';
$nb_post_type = \'post\';
// Post type page
if(is_page()){
if(nb_get_template_slug()){
$nb_post_name = nb_get_template_slug();
}else{
$nb_post_name = \'page\';
}
$nb_post_type = \'page\';
}
// Post type post / custom post
if(is_single()){
$nb_post_name = $post->post_type;
}
// View
if(is_admin()) $nb_view = \'back\';
// Before globals set default vars one last time
$globals_array = array(
\'nb_page\' => array(
\'id\' => $nb_id,
\'name\' => $nb_post_name,
\'type\' => $nb_post_type
)
);
/**
* Let the magin GLOBALS be registered
*/
foreach ($globals_array as $name => $value) {
global $$name;
$$name = $value;
}
}
add_action( \'template_redirect\', \'nb_setup_globals\' );
这也适用于后端(帖子/页面编辑等)。虽然还没有完全测试过。