我正在开发一个插件,该插件使用自定义重写结构注册自定义帖子类型,定义如下:
/**
* create_ldp_type - LDP Resource post type creation and registration
*
* @return {type} description
*/
public function create_ldp_type() {
register_post_type(\'ldp_resource\',
array(
\'labels\' => array(
\'name\' => __(\'Resources\', \'wpldp\'),
\'singular_name\' => __(\'Resource\', \'wpldp\'),
\'all_items\' => __(\'All resources\', \'wpldp\'),
\'add_new_item\' => __(\'Add a resource\', \'wpldp\'),
\'edit_item\' => __(\'Edit a resource\', \'wpldp\'),
\'new_item\' => __(\'New resource\', \'wpldp\'),
\'view_item\' => __(\'See the resource\', \'wpldp\'),
\'search_items\' => __(\'Search for a resource\', \'wpldp\'),
\'not_found\' => __(\'No corresponding resource\', \'wpldp\'),
\'not_found_in_trash\'=> __(\'No corresponding resource in the trash\', \'wpldp\'),
\'add_new\' => __(\'Add a resource\', \'wpldp\'),
),
\'description\' => __(\'LDP Resource\', \'wpldp\'),
\'public\' => true,
\'show_in_nav_menu\' => true,
\'show_in_menu\' => true,
\'show_in_admin_bar\' => true,
\'supports\' => array(\'title\'),
\'has_archive\' => true,
\'rewrite\' => array(\'slug\' => \'ldp/%ldp_container%\'),
\'menu_icon\' => \'dashicons-image-filter\',
));
}
我之前在一个
init
由于这似乎不是一个合适的解决方案,我决定使用
register_activation_hook
方法当我使用面向对象的方法时,我的所有挂钩都被包装并从对象构造函数调用:
/**
* Default Constructor
**/
public function __construct() {
register_activation_hook( __FILE__, array($this, \'wpldp_rewrite_flush\' ) );
register_deactivation_hook( __FILE__, array($this, \'wpldp_flush_rewrite_rules_on_deactivation\' ) );
register_activation_hook( __FILE__, array($this, \'generate_menu_item\') );
register_deactivation_hook( __FILE__, array($this, \'remove_menu_item\' ) );
// Entry point of the plugin
add_action(\'init\', array($this, \'wpldp_plugin_update\'));
add_action( \'init\', array($this, \'load_translations_file\'));
add_action( \'init\', array($this, \'create_ldp_type\'));
add_action( \'init\', array($this, \'add_poc_rewrite_rule\'));
}
我的
wpldp_rewrite_flush
方法如下:
public function wpldp_rewrite_flush() {
// Register post type to activate associated rewrite rules
$this->create_ldp_type();
$this->add_poc_rewrite_rule();
// Flush rules to be certain of the possibility to access the new CPT
flush_rewrite_rules( true );
}
我通过进行一些调试来确保执行此代码。我已经阅读了很多帖子,这些帖子都认为这个解决方案是最好的,但我的问题是我的自定义帖子类型无法访问(404错误)。
我尝试访问的URL类型为http://localhost/wordpress/ldp/actor/benoit-alessandroni/
, actor
作为自定义分类术语(aldp_container
. 使用呼叫flush_rewrite_rules()
就在我打电话给register_post_type
在create_ldp_type
这个方法工作得很好,但我知道这不好。
那么,你有没有看到任何明显的东西可以解释这个问题?
提前感谢,