WordPress is the most popular内容管理系统(CMS),并且有充分的理由 - 该平台对用户友好,直接且功能强大。

对于许多网站构建者而言,WordPress的最大好处之一就是能够使用默认WordPress部署中可用的标准帖子类型在几分钟内开始编辑和创建新帖子。

But what happens if you need a form or function that lies outside these typical WordPress post offerings? Armed with a bit of technical skill and the right knowledge, you can create custom post types that help your site stand out from the crowd. Here’s what you need to know.

Grow Your Business With HubSpot's Tools for WordPress Websites

What is a WordPress Custom Post Type?

官方WordPress网站lists seven post types

  • Posts
  • Pages
  • 附件
  • 修订
  • 导航菜单
  • 自定义CSS
  • 更改集

帖子是标准博客,以相反的顺序显示,页面更具结构化,通常具有亲子层次结构,附件保存有关任何媒体上传的信息,修订版用于创建发布历史记录,菜单让用户导航您的网站,自定义CSS可以帮助您修改网站的外观,更改集专门为WordPress Customizer设计。

但是,在许多情况下,这七种帖子类型不足以在WordPress网站上显示和管理特定内容。自定义帖子类型提供了一种创建和定义您自己的帖子首选项,标题和标签的方法,除了WordPress标准格式外,并帮助您的网站引起注意。

How Do I Create a WordPress Custom Post Type?

There are two ways to create a custom post type: Using a WordPress plugin or digging into the WordPress PHP code to create your post type manually.

Using a WordPress plugin is by far the easier and faster choice since you’ll be able to choose post parameters from within your WordPress dashboard and quickly deploy the exact type of custom post you want. The caveat? These posts only last as long as your WordPress plugin is active. If you choose to delete the plugin or it’s no longer supported by new WordPress versions, your custom post type won’t work.

That leaves building your post type manually — but how do you get started?

Step-by-Step: Creating a Custom WordPress Custom Post Type

  1. 将代码添加到您的函数.php文件
  2. Check your site to ensure the new post type appears
  3. Customize the code with additional functions

让我们更详细地探索每个步骤。

1. Add code to your function.php file.

As noted bywpbeginner,有一个标准代码字符串必须添加到您的函数.php文件中,以创建自定义帖子类型的基础:

// Our custom post type function

函数create_posttype(){

register_post_type('电影',

// CPT选项

array(

'labels'=> array(

'name'=> __('电影'),

'singular_name' => __( 'Movie' )

),

'public'=> true,

'has_archive'=> true,

'重写'=> array('slug'=>'电影'),

'show_in_rest'=> true,

);

}

//将我们的功能连接到主题设置

add_action('init','create_postType');

2. Check that your custom post type appears in your WordPress Dashboard.

在上面的示例中,自定义帖子类型称为“电影”。为了确保其按预期工作,请导航到WordPress仪表板。

If you’re successful, you’ll see a new category called “Movies” listed under “Comments”.

3.自定义您的新帖子类型。

Once you’ve confirmed the new post type is working, you can include additional functionality with this code:

/*

* Creating a function to create our CPT

*/

函数custy_post_type(){

//设置自定义帖子类型的UI标签

$ labels = array(

'name' => _x( 'Movies', 'Post Type General Name', 'twentytwenty' ),

'singular_name'=> _x('电影','post type单数名','twentytwenty'),

'menu_name' => __( 'Movies', 'twentytwenty' ),

'parent_item_colon'=> __('parent Movie','twentytwenty'),

'all_items' => __( 'All Movies', 'twentytwenty' ),

'view_item'=> __('查看电影','twentytwenty'),

'add_new_item' => __( 'Add New Movie', 'twentytwenty' ),

'add_new' => __( 'Add New', 'twentytwenty' ),

'edit_item'=> __('编辑电影','twentytwenty'),

'update_item' => __( 'Update Movie', 'twentytwenty' ),

'search_items' => __( 'Search Movie', 'twentytwenty' ),

'not_found'=> __('找不到','twentytwenty'),

'not_found_in_trash' => __( 'Not found in Trash', 'twentytwenty' ),

);

//为自定义邮政类型设置其他选项

$ args = array(

'label' => __( 'movies', 'twentytwenty' ),

'description'=> __('电影新闻和评论'bob官网官方网站,'twytwenty'),

'labels' => $labels,

// Features this CPT supports in Post Editor

'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),

//您可以将此CPT与分类法或自定义分类法相关联。

'分类法'=>阵列(“类型”),

/*分层cpt就像页面,可以

*父母和子项目。非层次结构CPT

* is like Posts.

*/

'elierarchical'=> false,

'public'=> true,

'show_ui'=> true,

'show_in_menu' => true,

'show_in_nav_menus'=> true,

'show_in_admin_bar' => true,

'menu_position'=> 5,

'can_export' => true,

'has_archive'=> true,

'exclude_from_search' => false,

'publicly_queryable' => true,

'capability_type' => 'post',

'show_in_rest'=> true,

);

//注册您的自定义帖子类型

register_post_type('电影',$ args);

}

/*Hook into the 'init' action so that the function

*包含我们的帖子类型注册不是

*不必要地执行。

*/

add_action('init','custom_post_type',0);

This allows you to add revisions, custom fields and parent/child category relationships to build out the functionality of your new custom post type.

尽管有某些限制 - 新帖子类型的名称不能超过20个字符,并且只能包括字母数字字符,但WordPress在创建自定义页面类型和添加新功能时允许实质性自由。

The WP_Query Class for Custom Post Types

随着您的自定义帖子类型变得更加复杂,并且拥有越来越多的页面,图像和其他媒体,因此值得考虑的WP_QUERY类,该类使您可以创建自定义的搜索条件。

让我们再次考虑我们的“电影”类别。集成此代码将返回电影类别中的任何帖子:

<?php

// The Query

$the_query = new WP_Query( 'category_name=movies' );

?>

To display the results of this query, developers must implement additional code:

<?php

// The Query

$the_query = new WP_Query( 'category_name=movies' );

//循环

if ( $the_query->have_posts() ) {

echo '

    ';

    while($ the_query-> has_posts()){

    $ the_query-> the_post();

    回声'

  • '。get_the_title()。'
  • ';

    }

    echo '

';

} 别的 {

//找不到帖子

}

/ *还原原始帖子数据 */

wp_reset_postdata();

?>

使用WP_QUERY,WordPress开发人员可以创建高级查询,从而从按需自定义页面返回特定结果。

WordPress Custom Post Type Plugins

如上所述,虽然可以手动创建自定义帖子类型,但使用WordPress插件创建和部署这些自定义类型要快得多。虽然这些帖子类型如果您删除插件或不再由最新版本的WordPress支持,但插件是开始自定义帖子类型并发现最适合您的插件的好方法WordPress网站

一些流行的WordPress自定义帖子类型插件包括:

自定义帖子类型UI

自定义帖子类型UIoffers a streamlined and simple interface to help create and manage custom post types on your WordPress site.

帖子类型无限

发布类型无限的主题可用,并让您快速创建自定义帖子类型和自定义分类法。

Toolset

工具集是一个付费,一体化的插件使it easy to create, manage and configure custom post types.

WPK自定义帖子类型和自定义字段创建者

该插件配备了三个工具 - 自定义字段创建者,邮政类型创建者和分类创建者 - 可帮助创建,管理和维护自定义帖子类型。

Flexibility in the Framework

WordPress提供功能强大的开箱即用性能,但是随着您的网站的发展,您可能需要形式和功能,因此基本的WordPress部署根本无法提供。在这种情况下,自定义帖子类型通常是在现有WordPress框架中增强网站功能的最简单方法。

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Use HubSpot tools on your WordPress website and connect the two platforms  without dealing with code. Click here to learn more.

Originally published Dec 29, 2020 7:00:00 AM, updated March 17 2021

主题:

WordPress网站