Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 为什么可以';我是否在类对象中添加register\u post\u类型或分类法?_Wordpress - Fatal编程技术网

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设置),就可以了。

这就是我的问题(答案有效)。为什么我每天都这么用。