Wordpress 为什么可以';我是否在类对象中添加register\u post\u类型或分类法?
代码如下:Wordpress 为什么可以';我是否在类对象中添加register\u post\u类型或分类法?,wordpress,Wordpress,代码如下: <?php // (wp_plugin_dir/My_plugin/index.php) class interface{ __construct(){ $this->get_commerce(); } } $interface = new interface(); // (wp_plugin_dir/My_plugin/commerce/commerce.php) class commerce{ __co
<?php
// (wp_plugin_dir/My_plugin/index.php)
class interface{
__construct(){
$this->get_commerce();
}
}
$interface = new interface();
// (wp_plugin_dir/My_plugin/commerce/commerce.php)
class commerce{
__construct(){
$this->register_post_types();
}
function register_post_types(){
$args = array( 'public' => true, 'label' => 'Books' );
register_post_type( 'book', $args );
}
}
}
?>
出于某种原因,它没有注册帖子类型?但是,当我在index.php中使用相同的register\u post\u类型函数时,它运行良好您可能正在尝试在寄存器初始化之前运行它。只需将类的方法挂接到init hook(或任何在它之后运行的东西,如theme\u设置),就可以了。这就是我的问题(答案有效)。为什么我每天都这么用。