wordpress 自定义_WordPress自定义帖子类型的权威指南

wordpress 自定义

Gone are the days when WordPress was referred to as just blogging software.

WordPress被称为博客软件的日子已经一去不复返了。

The ability to extend its functionality via plugins and themes, group posts or structured data into various types and also the arrival of the WP Rest API into core, has seen it further evolve into a full blown content management system and development platform.

通过插件和主题,组帖子或结构化数据将其功能扩展为各种类型的能力以及WP Rest API成为核心的能力,已经使其进一步发展成为功能完善的内容管理系统和开发平台。

Over the years, I have successfully developed a number of custom web applications built on-top WordPress that heavily utilize custom post types. An example of one of these web applications is the theme marketplace of my WordPress plugin called ProfilePress.

多年来,我已经成功开发了许多自定义Web应用程序,这些应用程序是在WordPress上构建的,这些应用程序大量使用自定义帖子类型。 这些Web应用程序之一就是我的WordPress插件ProfileProfile主题市场

On a closer look at the theme marketplace linked above, you will discover that it uses a custom pagination system evident in the URL of the Next / Prev pagination links. Thus, it is possible for you to build an in-house pagination system that will work perfectly in querying a custom post type (CPT).

仔细查看上面链接的主题市场,您会发现它使用了一个自定义分页系统,该系统明显显示在Next / Prev分页链接的URL中。 因此,您可以构建一个内部分页系统,该系统可以完美地查询自定义帖子类型 (CPT)。

Enough said, let’s get down to the purpose of this tutorial which is, learning the intricacies of custom post types in WordPress.

够了,让我们开始本教程的目的,即学习WordPress中自定义帖子类型的复杂性。

I hope to address the many hurdles I have personally encountered working with CPTs as well as some cool stuff you can do with it.

我希望解决我个人在使用CPT时遇到的许多障碍以及您可以使用的一些不错的方法。

自定义帖子类型定义 (Custom Post Type Definition)

WordPress can hold and display many different types of content. A single item of such content is generally called a post, although a post is also a specific post type.

WordPress可以保存和显示许多不同类型的内容。 这种内容的单个项目通常称为帖子,尽管帖子也是特定的帖子类型。

Internally, all the post types are stored in the same place, in the wp_posts database table, but are differentiated by a column called post_type.

在内部,所有帖子类型都存储在wp_posts数据库表中的同一位置,但由称为post_type的列来区分。

Post type refers to the various structured data grouped together that is maintained in the WordPress database posts table.

帖子类型是指在WordPress数据库posts表中维护的各种结构化数据分组在一起。

Examples of post types are post (a group of blog posts), page (a group of pages), attachment (a group of uploaded media files), and revision (a groups of post revisions) that are native or built-in to WordPress.

帖子类型的示例包括WordPress本地或内置的post (一组博客帖子), page (一组页面), attachment (一组上传的媒体文件)和revision (一组帖子修订)。 。

Armed with the definition of post type, a new post type that is created and registered to WordPress is referred to as a custom post type.

有了帖子类型的定义,创建并注册到WordPress的新帖子类型称为自定义帖子类型

If you’re building a company or business website with WordPress, examples of post types you could create are Portfolio, Testimonials and Products.

如果您要使用WordPress构建公司或商业网站,则可以创建的帖子类型示例包括投资组合,推荐和产品。

Now that we’ve understood the concept of custom post types, up next is learning how to create them.

现在我们已经了解了自定义帖子类型的概念,接下来是学习如何创建它们。

创建自定义帖子类型 (Creating Custom Post Types)

Creating a custom post type is pretty easy. Firstly, register the post type with the register_post_type() function and finally, wrap register_post_type() in a function call and hook it to the init Action like so:

创建自定义帖子类型非常简单。 首先,使用register_post_type()函数注册帖子类型,最后,在函数调用中包装register_post_type()并将其挂钩到init Action上,如下所示:

function portfolio_cpt() {

    $args = array(
        'label'  => 'Portfolio',
        'public' => true,
    );

    register_post_type( 'portfolio', $args );
}

add_action( 'init', 'portfolio_cpt' );

From the code above, you can see register_post_type() has a second function parameter that accepts a number of array arguments necessary in customizing every aspect of a custom post type.

从上面的代码中,您可以看到register_post_type()具有第二个函数参数,该参数接受自定义自定义帖子类型的各个方面所需的多个数组参数。

You should now see the Portfolio custom post type appear in WordPress admin dashboard.

现在,您应该会看到WordPress管理仪表板中显示“投资组合”自定义帖子类型。

Custom Post Type - Portfolio

It’s worth noting that when calling register_post_type(), the second argument is optional. That is, a CPT can be also created as follows:

值得注意的是,在调用register_post_type() ,第二个参数是可选的。 即,还可以如下创建CPT:

function portfolio_cpt() {

    register_post_type( 'portfolio' );
}

add_action( 'init', 'portfolio_cpt' );

If a CPT is created in this manner, it won’t show up in the admin dashboard menu (albeit still accessible via direct URL access “http://example.com/wp-admin/edit.php?post_type=portfolio“) and its UI wordings (otherwise referred to as labels) and admin notices will be the same as the built-in post post type.

如果以这种方式创建CPT,它将不会显示在管理控制台菜单中(尽管仍然可以通过直接URL访问“ http://example.com/wp-admin/edit.php?post_type=portfolio ”进行访问)其UI的字眼(或称为标签)和管理的通告都会一样内置的postStruts式。

Let’s go over some of the array arguments for customising CPTs and their respective functions.

让我们看一下一些用于自定义CPT及其各自功能的数组参数。

标签 (Label)

A plural descriptive name for your custom post type. For example, if you are creating a movie CPT, this should be Movies.

自定义帖子类型的复数形式描述性名称。 例如,如果要创建movie CPT,则应为Movies

It will default to the value of $post_type which is the first parameter of register_post_type().

它将默认为$post_type的值,该值是register_post_type()的第一个参数。

标签 (Labels)

An array of labels for this post type. Each string is a bit of text displayed in a particular admin page.

此帖子类型的标签数组。 每个字符串都是在特定管理页面中显示的一些文本。

Be sure to make these strings translatable if you’re creating a plugin for public use.

如果要创建供公众使用的插件,请确保使这些字符串可翻译

  • name: The plural form of the name of your post type.

    name :帖子类型名称的复数形式。

  • singular_name: The singular form of the name of your post type.

    singular_name :帖子类型名称的单数形式。

  • add_new: The menu item for adding a new post.

    add_new :用于添加新帖子的菜单项。

  • add_new_item: The header shown when creating a new post.

    add_new_item :创建新帖子时显示的标题。

  • edit_item: The header shown when editing a post.

    edit_item :编辑帖子时显示的标题。

  • new_item: Shown in the favorites menu in the admin header.

    new_item :显示在管理员标题的“收藏夹”菜单中。

  • view_item: Shown alongside the permalink on the edit post screen.

    view_item :显示在编辑帖子屏幕上的永久链接旁边。

  • search_items: Button text for the search box on the edit posts screen.

    search_items :编辑帖子屏幕上搜索框的按钮文本。

  • not_found: Text to display when no posts are found through search in the admin.

    not_found :在管理员中通过搜索未找到任何帖子时显示的文本。

  • not_found_in_trash: Text to display when no posts are in the trash.

    not_found_in_trash :垃圾桶中没有帖子时显示的文本。

A full list of labels and their descriptions can be found here.

标签及其说明的完整列表可在此处找到

描述 (description)

A short descriptive summary of what the post type is, although I haven’t found where this is used in WordPress admin.

关于帖子类型的简短描述性摘要,尽管我还没有找到WordPress管理员在哪里使用它。

上市 (public)

Depending on its Boolean value, it’ll automatically decide what other arguments should be unless they’re specifically defined. If you’re looking to have more control over the public arguments, there are three specific arguments you may set:

取决于其布尔值,除非特别定义,否则它将自动决定应使用的其他参数。 如果您希望对公共参数有更多的控制权,则可以设置三个特定的参数:

  • show_ui: determines whether to show the administration screens.

    show_ui:确定是否显示管理屏幕。

  • publicly_queryable: determines whether queries for this post type can be performed from the front end.

    publicly_queryable:确定是否可以从前端执行对此帖子类型的查询。

  • exclude_from_search: whether the posts should appear in search results.

    exclude_from_search:帖子是否应出现在搜索结果中。

By default, a new post type is added after the ‘Comments’ menu item in the admin. But you have to ability to move it to a suitable position of your choosing.

默认情况下,在管理员的“评论”菜单项之后添加新的帖子类型。 但是您必须能够将其移动到您选择的合适位置。

For example, setting the menu_position value to 70 will add your menu item below Users.

例如,将menu_position值设置为70将在“ 用户”下方添加菜单项。

New post types will default to the Posts menu icon, but if you want a custom icon in it instead, set this label to the URL of the icon or image file.

新的帖子类型将默认为“帖子”菜单图标,但是如果您希望在其中使用自定义图标,请将此标签设置为图标或图像文件的URL。

'menu_icon' => get_stylesheet_directory_uri() . '/images/portfolio-icon.png',

You can also use any dashicon as your CPT icon.

您也可以将任何破折号用作CPT图标。

Say you want to use the download dashicon, set this label to the dashicon value as follows:

假设您要使用下载破折号 ,请将此标签设置为破折号值,如下所示:

'menu_icon' => 'dashicons-download',

等级制 (hierarchical)

This argument allows you to decide whether to make your CPT hierarchical or not. The default value is false. Set to true to make your CPT hierarchical.

此参数使您可以决定是否使CPT分层。 默认值为false 。 设置为true可使CPT分层。

支持 (supports)

The supports argument allows you to define an array of meta boxes and fields that will appear on the screen when editing or creating a new post. This defaults to title and editor.

supports参数可让您定义在编辑或创建新帖子时将在屏幕上显示的元框和字段的数组。 默认为标题和编辑器。

Setting this argument to false will prevent the default (title and editor) behavior.

将此参数设置为false将阻止默认的行为(标题和编辑器)。

There are several available options:

有几种可用的选项:

  • title: Text input field to create a post title.

    title :用于输入帖子标题的文本输入字段。

  • editor: Content TinyMCE editor for writing.

    editor :内容TinyMCE编辑器,用于编写。

  • author: A select box for changing the post author.

    author :用于更改帖子作者的选择框。

  • thumbnail: Featured image capability.

    thumbnail :特色图像功能。

  • excerpt: A textarea for writing a custom excerpt.

    excerpt :用于编写自定义摘录的文本区域。

  • trackbacks: Ability to turn trackbacks and pingbacks on/off.

    trackbacks :能够打开/关闭引用和pingback。

  • custom-fields: Custom fields input field.

    custom-fields :自定义字段输入字段。

  • comments: Turn comments on/off.

    comments :打开/关闭评论。

  • revisions: Allows revisions to be made of your post.

    revisions :允许对您的帖子进行修订。

  • post-formats: Add post formats, see the ‘Post Formats’ section

    post-formats :添加帖子格式,请参见“帖子格式”部分

  • page-attributes: The attributes box shown for pages. This is important for hierarchical post types, so you can select the parent post.

    page-attributespage-attributes的属性框。 这对于分层帖子类型很重要,因此您可以选择父帖子。

register_meta_box_cb (register_meta_box_cb)

Provides a callback function that will be called when setting up the meta boxes for the edit form. The callback function takes one argument $post, which contains the WP_Post object for the currently edited post.

提供一个回调函数,在为编辑表单设置元框时将调用该函数。 回调函数采用一个参数$post ,其中包含当前编辑的帖子的WP_Post对象。

This feature is particularly useful to developers for them to create custom meta boxes that will show up in the CPT edit screen.

此功能对开发人员而言特别有用,因为他们可以创建自定义元框,这些元框将显示在CPT编辑屏幕中。

'register_meta_box_cb' => 'metabox_callback_func',

分类法 (taxonomies)

An array of registered taxonomies like category or post_tag that will be used with this custom post type.

一组已注册的分类法(如categorypost_tag将与该自定义帖子类型一起使用。

'taxonomies' => array( 'post_tag', 'category '),

has_archive (has_archive)

Setting this argument to true will enable archives for your custom post type. For example, say your CPT is books, visiting http://yoursite.com/books will display a list of posts belonging to books custom post type.

将此参数设置为true将为您的自定义帖子类型启用存档。 例如,假设您的CPT是books ,那么访问http://yoursite.com/books将显示属于books自定义帖子类型的帖子列表。

改写 (rewrite)

This argument allows you to define the permalink structure of your custom post type when viewing a single post or archive.

使用此参数,可以在查看单个帖子或档案时定义自定义帖子类型的永久链接结构。

Default value is true and uses $post_type as slug. To prevent rewrites, set to false.

默认值为true并使用$post_type作为$post_type 。 为防止重写,请设置为false

Let’s see some examples for clarity sake.

为了清楚起见,让我们看一些示例。

Say you created a review custom post type but wish to change the URL slug from review to assessment, using the following rewrite argument will change the URL from http://example.com/review/harry-potter/ to http://example.com/assessment/harry-potter/ for single posts and http://example.com/review/ to http://example.com/assessment/ for the CPT archive.

假设您创建了review自定义帖子类型,但希望将URL条目从评论更改为评估 ,则使用以下重写参数会将URL从http://example.com/review/harry-potter/更改为http://example.com/assessment/harry-potter/针对单个帖子),以及http://example.com/review/http://example.com/assessment/针对CPT存档)。

'rewrite' => array(
    'slug'       => 'assessment',
    'with_front' => false
),

Whenever you do a rewrite of WordPress URL, ensure you click the Save Changes button in Settings >> Permalinks to recreate the rewrite rules.

每当您重写WordPress URL时,请确保单击设置>>永久链接中Save Changes按钮以重新创建重写规则。

So basically, the slug defines the new URL slug while with_front determines if the permalink structure be pre-pended with the front base.

因此,基本上,该slug定义了新的URL蛞蝓而with_front确定是否固定链接结构可以与前基预挂起。

Still not clear on with_front? Let’s see an example.

仍然不清楚with_front吗? 让我们来看一个例子。

Say you have a permalink structure with blog appended to the URL as depicted in the image below.

假设您有一个永久链接结构,其中Blog附加到了URL,如下图所示。

Custom Post Type - Common Settings

If with_front is set to false, the URL of a single post and post archive becomes http://example.com/blog/assessment/harry-potter/ and http://example.com/blog/assessment/ respectively but if it is set to true, the URL of a single post and post archive respectively becomes http://example.com/assessment/harry-potter/ and http://example.com/assessment/.

如果with_front设置为false,则单个帖子和帖子存档的URL分别变为http://example.com/blog/assessment/harry-potter/http://example.com/blog/assessment/设置为true ,单个帖子和帖子存档的URL分别变为http://example.com/assessment/harry-potter/http://example.com/assessment/

Notice the omission of blog in the latter? That’s the difference.

注意到后者中blog的遗漏? 就是这样。

can_export (can_export)

Use this argument to decide whether posts belonging to your custom post type can be exportable via the WordPress export tool. By default, this is set to true.

使用此参数来确定属于您自定义帖子类型的帖子是否可以通过WordPress导出工具导出。 默认情况下,它设置为true

query_var (query_var)

This argument allows you to control the query variable used to get posts of this type.

此参数允许您控制用于获取此类型帖子的查询变量。

If set to true, it will allow you to request a book custom posts type via example.com/?book=harry-potter where harry-potter that is the URL slug of a book entry or post.

如果设置为true ,它将允许您通过example.com/?book=harry-potter请求书籍自定义帖子类型,其中harry-potter是书籍条目或帖子的URL段。

If set to a string rather than true (for example publication), you can do: example.com/?publication=harry-potter.

如果将其设置为字符串而不是true(例如publication ),则可以执行: example.com/?publication=harry-potter ? publication = example.com/?publication=harry-potter

警告“ query_var” (Caveat of “query_var”)

If query_var is undefined in your CPT registration array argument, it defaults to $post_type thus, it is always defined unless you set it to false.

如果在CPT注册数组参数中未定义query_var ,则它默认为$post_type因此,除非将其设置为false,否则它将始终被定义。

Here comes the caveat. Whenever the value of a query_var is added as a query string to a URL, it will lead to a 404.

警告。 每当将query_var的值作为查询字符串添加到URL时,它将导致404。

Let me clarify. Say the value of your CPT query_var is review and a query string with the key set to review was added to any URL of your WordPress site in any of the following forms:

让我澄清一下。 假设您的CPT query_var值为“ review并且以以下任意一种形式将具有设置为“ review”的键的查询字符串添加到WordPress网站的任何URL:

This will cause a 404 to happen.

这将导致404发生。

I actually learned about this the hard way. As at the time I had this issue, I created a ticket in WordPress core trac to report it as a bug.

我实际上是通过艰难的方式了解到的。 在我遇到这个问题时,我在WordPress核心跟踪中创建了一个票证,将其报告为错误。

It took me weeks to finally figure it out before some members of the core WordPress team replied my ticket.

我花了好几个星期才终于弄明白,然后WordPress核心团队的一些成员回答了我的问题。

使用插件加快自定义帖子类型的设置 (Speeding up Custom Post Types Setup with Plugins)

Now that we’ve covered the foundations, it’s a good time to point out that there are a number of plugins in WordPress plugin repository that makes creating custom post types very easy.

既然我们已经介绍了基础,是时候指出WordPress插件存储库中有许多插件,使创建自定义帖子类型变得非常容易了。

Examples include (but are not limited to):

示例包括(但不限于):

结论 (Conclusion)

In this tutorial, we learned what custom post types are and how they’re created.

在本教程中,我们了解了什么是自定义帖子类型以及如何创建它们。

This is actually the first in a series on WordPress custom post types. In my next tutorial, we will learn how to customize the various admin notices, how to register custom taxonomies to a post type and adding contextual help tab to your CPT screen.

这实际上是有关WordPress自定义帖子类型的系列文章中的第一篇。 在我的下一个教程中,我们将学习如何自定义各种管理员通知,如何将自定义分类法注册到帖子类型以及如何在CPT屏幕上添加上下文帮助选项卡。

Until then, happy coding!

在此之前,祝您编程愉快!

翻译自: https://www.sitepoint.com/definitive-guide-to-wordpress-custom-post-types/

wordpress 自定义

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值