我通常使用这样的方式:
<?php
/*
Plugin Name: My Plugin
Plugin URI: https://my.plugin.com
Description: My plugin description
Version: 1.0.0
Author: Me
Author URI: https://my.website.com
License: GPL2 (or whatever)
Notes:
*/
class myClass {
function __construct() {
add_action( \'init\', array( $this, \'create_post_type\' ) );
}
function create_post_type() {
$name = \'Foos\';
$singular_name = \'Foo\';
register_post_type(
\'rg_\' . strtolower( $name ),
array(
\'labels\' => array(
\'name\' => _x( $name, \'post type general name\' ),
\'singular_name\' => _x( $singular_name, \'post type singular name\'),
\'menu_name\' => _x( $name, \'admin menu\' ),
\'name_admin_bar\' => _x( $singular_name, \'add new on admin bar\' ),
\'add_new\' => _x( \'Add New\', strtolower( $name ) ),
\'add_new_item\' => __( \'Add New \' . $singular_name ),
\'new_item\' => __( \'New \' . $singular_name ),
\'edit_item\' => __( \'Edit \' . $singular_name ),
\'view_item\' => __( \'View \' . $singular_name ),
\'all_items\' => __( \'All \' . $name ),
\'search_items\' => __( \'Search \' . $name ),
\'parent_item_colon\' => __( \'Parent :\' . $name ),
\'not_found\' => __( \'No \' . strtolower( $name ) . \' found.\'),
\'not_found_in_trash\' => __( \'No \' . strtolower( $name ) . \' found in Trash.\' )
),
\'public\' => true,
\'has_archive\' => strtolower($taxonomy_name),
\'hierarchical\' => false,
\'rewrite\' => array( \'slug\' => $name ),
\'menu_icon\' => \'dashicons-carrot\'
)
);
}
}
$my_class = new myClass();
?>
您可以致电
add_action
只要操作是init回调的一部分,就可以将函数转换为另一个函数。
请注意add_action
函数在类中放置时使用数组而不是字符串。
如果需要,您可以将其用作插件中的主文件。
codex没有显示使用变量$name
和$singular_name
, 但我喜欢这样开始,以便在开发过程中随时可以轻松更改它。
以下是有关创建帖子类型的详细信息:https://developer.wordpress.org/reference/functions/register_post_type/https://codex.wordpress.org/Function_Reference/register_post_type