请尝试以下代码:
class SearchFilters
{
public $post_type_slug;
public $post_type_label_name;
public $post_type_singular_name;
public $current_plugin_domain;
public function __construct( $post_type_slug, $post_type_label_name, $post_type_singular_name )
{
$this->current_plugin_domain = get_current_plugin_domain();
$this->post_type_slug = $post_type_slug;
$this->post_type_label_name = $post_type_label_name;
$this->post_type_singular_name = $post_type_singular_name;
$this->init();
}
private function init(){
$filters_slug = "filters-" . $this->post_type_slug;
$filters_args = [
\'labels\' => array(
\'name\' => sprintf( __( "Filters - Search Form for %s", $this->current_plugin_domain ), $this->post_type_label_name ),
\'singular_name\' => __( "Filter", $this->current_plugin_domain ),
\'menu_name\' => __( "Filters", $this->current_plugin_domain ),
\'add_new_item\' => sprintf( __( "Filters - Add New Search Form for %s", $this->current_plugin_domain ), $this->post_type_singular_name )
),
\'description\' => __( "Filters to display search form on front end", $this->current_plugin_domain ),
\'supports\' => array( \'title\' ),
\'public\' => false,
\'show_ui\' => true,
\'exclude_from_search\' => true,
\'show_in_admin_bar\' => false,
\'show_in_nav_menus\' => false,
\'publicly_queryable\' => false,
\'query_var\' => false,
\'show_ui\' => true,
\'menu_icon\' => \'dashicons-tag\',
\'capability_type\' => \'post\',
\'rewrite\' => array( \'slug\' => $this->post_type_slug),
\'show_in_menu\' => "edit.php?post_type=" . $this->post_type_slug,
\'auto_save\' => false
];
$result_filter = register_post_type(
$filters_slug,
$filters_args
);
if ( is_wp_error( $result_filter ) ) {
wp_error_log( $result_filter->get_error_message(), "Filter Post type creation " . "[" . __CLASS__ . "]" );
}
}
}