在"功能插件中注册自定义帖子类型的分类法"导致错误500

时间:2018-05-02 00:36:53

标签: php wordpress

我将功能从主题的functions.php移动到"功能插件"。出于某种原因,在functions.php中,一切都运行得很好。但是当我将它移动到插件中时,我得到整个wp-admin区域的错误500。

我能够通过逐个排除功能块来查明分类法注册代码块的问题。

以下是整个自定义帖子类型注册码:

function create_post_type() {
  register_post_type( 'Ads',
    array(
      'labels' => array(
        'name'               => 'Ads',
        'singular_name'      => 'Ad',
        'menu_name'          => 'Ads',
        'name_admin_bar'     => 'Ad',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Ad',
        'new_item'           => 'New Ad',
        'edit_item'          => 'Edit Ad',
        'view_item'          => 'View Ad',
        'all_items'          => 'All Ads',
        'search_items'       => 'Search Ads',
        'parent_item_colon'  => 'Parent Ads:',
        'not_found'          => 'No ads found.',
        'not_found_in_trash' => 'No ads found in Trash.'
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => true,
      'hierarchical' => true,
      'supports' => array( 
        'title', 
        'revisions'
    ),
    )
  );
}
/* This piece bellow is causing trouble*/
register_taxonomy("Placements", array("ads"), array(
    "hierarchical" => true,
    "label" => "Placements",
    "singular_label" => "Placement",
    "rewrite" => true
));
/* End of trouble making code */
add_action( 'init', 'create_post_type' );

这是错误日志

  

[Tue May 01 23:11:04.268105 2018] [:error] [pid 24511] [client 174.214.1.78:15113] PHP致命错误:未捕获错误:在/ nas中调用null成员函数add_rewrite_tag() /content/live/adsplugintest/wp-includes/rewrite.php:172\nStack trace:\ n#0 /nas/content/live/adsplugintest/wp-includes/class-wp-taxonomy.php(379):add_rewrite_tag( '%展示位置%','([^ /] +)','展示位置=')\ n#1 / nas / content / live / adsplugintest / wp-includes / taxonomy.php(386):WP_Taxonomy-> add_rewrite_rules()\ n#2 /nas/content/live/adsplugintest/wp-content/plugins/custom-ads-plugin/custom-ads.php (45):register_taxonomy(' Placements',Array,Array)\ n#3 /nas/content/live/adsplugintest/wp-settings.php(305):include_once(' / nas / content / li ...')\ n#4 /nas/content/live/adsplugintest/wp-config.php(121):require_once(' / nas / content / li ...&# 39;)\ n#5 /nas/content/live/adsplugintest/wp-load.php(37):require_once(' / nas / content / li ...')\ n#6 / NAS /内容/现场/ adsplugintest /可湿性粉剂管理员/ ADM in.php(31):require_once(' / nas / content / li ...')\ n#7 /nas/content/live/adsplugintest/wp-admin/index.php(10) :require_once(' / nas / content / li ...')\ n#8 {main} \ n在/nas/content/live/adsplugintest/wp-includes/rewrite.php中投放在线172

1 个答案:

答案 0 :(得分:0)

您需要在register_taxonomy()操作中调用init方法(正如您为register_post_type()所做的那样。

更新的代码将是:

function create_post_type() {
    register_post_type( ... );
    register_taxonomy( ... );
}
add_action( 'init', 'create_post_type' );

目前,您直接拨打register_taxonomy()而不是通过init操作,因此WordPress会很快解雇它。

register_taxonomy()的WordPress Codex也提到了这一点:

  

使用init操作来调用此功能。在操作之外调用它可能会导致麻烦。有关详细信息,请参阅#15568

相关问题