我相信你指的是"Post Types". 默认情况下,WordPress有一个您已经熟悉的完整帖子类型-post
(新闻/博客),page
(页),attachment
(媒体),revision
(第页/修订后)。您可以在上阅读内置帖子类型的完整列表The Codex - Post Types.
如果你想注册一个新的自定义帖子类型(CPT),你可以用一些PHP知识轻松地完成。让我们浏览并创建我们自己的帖子类型“Products”。
Step One - 连接到FTP-您do not 要使用Appearance -> Editor
.
Step Two - 找到您的主题,如果您使用的是子主题,请打开它并找到functions.php
文件在中打开文件Notepad++ 或您选择的编辑器。
Step Three - 注册您的帖子类型
朝着你的functions.php
我们要将文件挂接到WordPress中init
并使用一个内置的WordPress函数,巧妙地称为:register_post_type
. 我强烈建议您阅读这些法典链接,因为我不会解释每一点,而是给出一个简短的描述。
/** Create Post Type **/
function cpt_init() {
// Products Custom Post Type
register_post_type( \'cpt_products\', array( // Internal name `cpt_products` - this is what we test against in queries
\'labels\' => array( // An array of Administrative labels
\'name\' => __( \'Products\' ),
\'singular_name\' => __( \'Product\' ),
\'all_items\' => __( \'View Products\' ),
\'add_new\' => __( \'New Product\' ),
\'add_new_item\' => __( \'New Product\' ),
\'edit_item\' => __( \'Edit Product\' ),
\'view_item\' => __( \'View Product\' ),
\'search_items\' => __( \'Search Products\' ),
\'no_found\' => __( \'No Products Found\' ),
\'not_found_in_trash\' => __( \'No Products in Trash\' )
),
\'public\' => true, // Whether it will be publically available or only in the admin panel
\'publicly_queryable\'=> true, // " "
\'show_ui\' => true, // Whether we want this post type to show up in the admin panel
\'show_in_nav_menus\' => false, // If we want Products to show up in the `Appearance -> Menu`
\'capability_type\' => \'page\', // Mostly used for user capability
\'hierarchical\' => false, // If you will allow products to have child products / assigned a parent
\'rewrite\' => array( \'slug\' => \'product\', \'with_front\' => false ), // This is going to be what the single post will be located at `/product/product-name/`
\'menu_icon\' => \'dashicons-cart\', // https://developer.wordpress.org/resource/dashicons/#welcome-widgets-menus
\'menu_position\' => 5, // Where you want it to show up in the admin panel
\'supports\' => array( \'title\', \'editor\', \'page-attributes\', \'revisions\' ) // What is actually shown when a new product is added - admin panel
) );
}
add_action( \'init\', \'cpt_init\' );
Step Four - 上载
functions.php
- 这里的一个重要提示是
flush permalinks 这很容易
Settings -> Permalinks
然后单击页面底部的“保存”按钮。
类似于创建帖子类型,如果要创建类别(类似于帖子类别),可以使用register_taxonomy
作用按照上述步骤操作,直到步骤3。
Step 3 - 注册分类法
前两个参数对于将分类法链接到post类型很重要。下一个重要部分是hierarchical
参数-这将是post_tags
(错误)和categories
(正确)。我们将使这种分类法具有层次结构,类似于帖子类别。
/** Add Custom Taxonomy **/
function tax_init() {
// Product Categories
register_taxonomy(
\'tax_products\', // Taxonomy slug
\'cpt_products\', // What post type we\'re assigning the taxonomy to.
array( // Array of Arguments
\'labels\' => array( // User Friendly Labels
\'name\' => __( \'Product Categories\' ),
\'singular_name\' => __( \'Product Category\' ),
\'search_items\' => __( \'Search Product Categories\' ),
\'all_items\' => __( \'All Product Categories\' ),
\'parent_item\' => __( \'Parent Product Category\' ),
\'parent_item_colon\' => __( \'Parent Product Category:\' ),
\'edit_item\' => __( \'Edit Product Category\' ),
\'update_item\' => __( \'Update Product Category\' ),
\'add_new_item\' => __( \'Add New Product Category\' ),
\'new_item_name\' => __( \'New Product Category\' ),
\'menu_name\' => __( \'Product Categories\' )
),
\'public\' => true, // Whether it will be public or only used in the admin panel
\'hierarchical\' => true, // The differnce between *Tags* and *Categories*
\'show_in_nav_menus\' => true, // To show up in `Appearance -> Menus` or not.
\'show_admin_column\' => true, // If we want an column next to the title when listing our products
\'rewrite\' => array( \'slug\' => \'products/category\', \'with_front\' => false, \'hierarchical\' => true ) // Similar to post type, where to find the categories `products/category/category-1/`
)
);
}
add_action( \'init\', \'tax_init\');