每次更改重写规则时,都需要在数据库中重建重写规则。你能做到的flush_rewrite_rules()
函数,但不要在每次页面加载中都使用它,您只需要执行一次。一般来说,一个很好的钩子就是register_actionvation_hook()
插件的。当您的插件被停用时,您还应该重建重写规则,以便删除自定义重写规则:
register_activation_hook( __FILE__, function () {
cyb_register_vmgallery_post_type();
flush_rewrite_rules();
} );
register_deactivation_hook( __FILE__, function () {
flush_rewrite_rules();
} );
add_action( \'init\', \'cyb_register_vmgallery_post_type\' );
function cyb_register_vmgallery_post_type() {
$labels = array(
\'name\' => __(\'VmGallery\', THEME_TEXT_DOMAIN),
\'singular_name\' => __(\'VmGallery\', THEME_TEXT_DOMAIN),
\'add_new\' => __(\'Add New\', THEME_TEXT_DOMAIN),
\'add_new_item\' => __(\'Add New Gallery\', THEME_TEXT_DOMAIN),
\'edit_item\' => __(\'Edit Gallery\', THEME_TEXT_DOMAIN),
\'new_item\' => __(\'New Gallery\', THEME_TEXT_DOMAIN),
\'all_items\' => __(\'All Galleries\', THEME_TEXT_DOMAIN),
\'view_item\' => __(\'View Gallery\', THEME_TEXT_DOMAIN),
\'search_items\' => __(\'Search Gallery\', THEME_TEXT_DOMAIN),
\'not_found\' => __(\'No galleries found\', THEME_TEXT_DOMAIN),
\'not_found_in_trash\' => __(\'No galleries found in the Trash\', THEME_TEXT_DOMAIN),
\'parent_item_colon\' => \'\',
\'menu_name\' => __(\'Gallery\', THEME_TEXT_DOMAIN),
);
$supports = array(\'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\', \'post-formats\', \'author\', \'excerpt\');
$args = array(
\'labels\' => $labels,
\'description\' => \'Galleries specific information\',
\'public\' => true,
\'menu_position\' => 6,
\'menu_icon\' => \'dashicons-format-image\',
\'supports\' => $supports,
\'taxonomies\' => array(\'gallery_categories\', \'post_tag\'),
\'has_archive\' => \'ggg\'
/*\'rewrite\' => array(
\'slug\' => $slug,
\'with_front\' => false
),*/
);
register_post_type(\'vmgallery\', $args);
}