【问题标题】:Cannot redeclare create_post_type() (previously declared无法重新声明 create_post_type()(之前声明过
【发布时间】:2018-08-31 20:45:09
【问题描述】:

在一个 wp 安装中运行 2 个非常相似的插件。当尝试激活一个新的时,我收到“无法重新声明以前声明的”错误。错误是指类似的插件之一。但是帖子类型实际上是不同的(代码如下)。服务器缓存已清除。

新帖子类型

function create_post_type() {
  register_post_type( 'Related',
    array(
      'labels' => array(
        'name'               => 'Related',
        'singular_name'      => 'Related Unit',
        'menu_name'          => 'Related Units',
        'name_admin_bar'     => 'Related Unit',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Unit',
        'new_item'           => 'New Unit',
        'edit_item'          => 'Edit Unit',
        'view_item'          => 'View Unit',
        'all_items'          => 'All Units',
        'search_items'       => 'Search Units',
        'parent_item_colon'  => 'Parent Units:',
        'not_found'          => 'No units found.',
        'not_found_in_trash' => 'No units found in Trash.'
      ),
      'public' => true,
      'has_archive' => true,
      'rewrite' => true,
      'hierarchical' => true,
      'supports' => array( 
        'title', 
        'revisions'
    ),
    )
  );
  register_taxonomy("RelatedPlacements", array("Related"), array(
    "hierarchical" => true,
    "label" => "Related Placements",
    "singular_label" => "Related Placement",
    "rewrite" => true
    ));
}

add_action( 'init', 'create_post_type' ); 

一个错误是指

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'
    ),
    )
  );
  register_taxonomy("Placements", array("ads"), array(
    "hierarchical" => true,
    "label" => "Placements",
    "singular_label" => "Placement",
    "rewrite" => true
    ));
}

add_action( 'init', 'create_post_type' );

我只是看不出它们有什么相同

【问题讨论】:

标签: php wordpress


【解决方案1】:

您不能在 PHP 中定义两个具有相同名称的函数。更改其中一个函数名称:

function create_related_post_type() {
   ...
}

add_action( 'init', 'create_related_post_type' );

由于 Wordpress 的程序代码库和插件冲突的可能性很大,通常建议为您的函数使用唯一名称作为前缀,例如 pluginname_create_post_type

【讨论】:

  • 做到了。谢谢!必须等待 7 分钟才能接受。
猜你喜欢
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
  • 2017-07-23
  • 2013-08-07
  • 2018-06-28
相关资源
最近更新 更多