wordpress 自定义_WordPress自定义帖子类型:它是什么以及如何创建?

wordpress 自定义

WordPress has come a long way – from being known as just a great blogging platform – and is today touted as the most popular content management system. WordPress took years to establish itself as a strong contender in the CMS industry, but what actually proved a game changer was the introduction of “Custom Post Types”. This mechanism was rolled out with WordPress version 3.0, and made it possible for users to create a wide variety of content.

WordPress已经走了很长的路要走-从被称为一个很棒的博客平台-到今天被誉为最受欢迎的内容管理系统。 WordPress花费了数年的时间才能确立自己在CMS行业中的有力竞争者的地位,但实际上证明了改变游戏规则的是引入了“自定义帖子类型”。 此机制已在WordPress 3.0版中推出,使用户可以创建各种内容。

This post will provide useful insights on Custom Post Type and how you can create one tailored to your specific needs.

这篇文章将提供有关“自定义帖子类型”的有用见解,以及如何创建满足您特定需求的帖子。

什么是自定义帖子类型? (What is Custom Post Type?)

Custom post types are just like your regular posts except that they have different post_type values. Although, WordPress has evolved from being used as a blogging platform into a full-fledged CMS, but some people mistakenly classify “Post Type” as simply a post. But, it can be any kind of content.

自定义帖子类型与常规帖子一样,只是它们具有不同的post_type值。 尽管WordPress已经从用作博客平台发展为成熟的CMS,但有人错误地将“帖子类型”归类为帖子。 但是,它可以是任何种类的内容。

WordPress comes with some default post types like Post, Page, Revision, etc. However, you can even create your own custom post types. For example, if you’re running a Photography WordPress website, then you can create a ‘Photography’ post type. You can even create custom post types for Testimonials, Reviews, etc.

WordPress带有一些默认的帖子类型,如“帖子”,“页面”,“修订”等。但是,您甚至可以创建自己的自定义帖子类型。 例如,如果您正在运行摄影WordPress网站,则可以创建“摄影”帖子类型。 您甚至可以为推荐书,评论等创建自定义帖子类型。

Understanding the Purpose Behind Creating Custom Post Types

了解创建自定义帖子类型背后的目的

Let’s assume, you would like to segregate your content on your site in a structured manner, for instance, you might want to define different categories within your blog, or wish to add posts by multiple authors and lot more. But, what if you want more that cannot be achieved with help of default post types? You can add possibly anything with help of a custom post type.

让我们假设,您想以一种结构化的方式隔离您网站上的内容,例如,您可能想在博客中定义不同的类别,或者希望添加多位作者的帖子以及更多内容。 但是,如果您希望借助默认帖子类型无法实现更多目标,该怎么办? 您可以借助自定义帖子类型添加任何内容。

如何创建自定义帖子类型? (How You Can Create a Custom Post Type?)

There are two different ways following which you can create a custom post type. Let us take a look at both these ways:

您可以通过两种不同的方法来创建自定义帖子类型。 让我们看一下这两种方式:

  1. Creating Custom Post Types Via Plugin

    One of the easiest trick or way that will help in creating a custom post type requires you to use a plugin. Especially, novices or non-programmers find this an ideal way to accomplish their goals.

    Custom Post Type UI is an excellent plugin that helps to create as well as manage custom posts types in your WordPress admin menu. It is compatibile with WordPress version 3.5 or higher. You can search for this plugin from the official WordPress Plugin Repository. And then, activate and install this plugin by following the below mentioned steps:

    • Open your WordPress dashboard, go to Plugins > Add New. And then, Search for Custom Post Type UI.
    • Click on the “Activate Plugin” link as shown in the screenshot below.
    • The Custom Post Type UI will get added to your admin menu.

    通过插件创建自定义帖子类型

    帮助创建自定义帖子类型的最简单的技巧或方法之一就是您需要使用插件。 尤其是,无论是新手还是非程序员,都可以找到实现目标的理想方法。

    自定义帖子类型用户界面是一个出色的插件,可帮助您在WordPress管理菜单中创建和管理自定义帖子类型。 它与WordPress 3.5或更高版本兼容。 您可以从官方WordPress插件存储库中搜索此插件。 然后,按照以下提到的步骤激活并安装此插件:

    • 打开WordPress信息中心,转到“插件”>“添加新内容”。 然后,搜索“自定义帖子类型” UI。
    • 单击“激活插件”链接,如下面的屏幕截图所示。
    • 自定义帖子类型用户界面将添加到您的管理菜单中。
  2. Creating Custom Post Type Manually

    Though WordPress plugins provide a hassle-free way to create custom post types, but there is a downside to using a plugin: your custom post types will no longer be visible once the plugin is deactivated. Although, the data of those custom post types will still exist, but you won’t be able to access your custom post type as it will become unregistered. In that case manually creating a custom post type will prove a viable alternative.

    For creating a custom post type, we’ll need to register our custom post type using the register_post_type() function. For example, let us create a code that register “Movies” as our custom post type. This code should be added to the function.php file:

    // Our custom post type function
    function create_posttype() {
    register_post_type( 'movies',
    // CPT Options
    array(
    'labels' => array(
    'name' => __( 'Movies' ),
    'singular_name' => __( 'Movie' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'movies'),
    );
    }
    
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype' );

    The above code will create a post type “Movies”. Next, we’ve defined an with an array with a few arguments that serves as the options of our custom post type. As you can see, the array is divided into two different parts. The first part contains some labels which define the custom post type name in two forms: singular and plural. The second part consists of other arguments, which includes public visibility, has_archive, and slug that will be used in your post type URLs.

    You can choose to add even more options to your custom post type, in order to enhance its capabilities.

    How to Display Custom Post Types on Your Site?

    As we all know, WordPress provides built-in support for different features. Likewise, it even provide support to display your custom post types. There are a lot of ways using which you can display your post types. In our case, we will talk about how you can showcase them on your front page. For this, add the following code in your theme’s functions.php file:

    手动创建自定义帖子类型

    尽管WordPress插件提供了一种轻松的方式来创建自定义帖子类型,但是使用插件有一个缺点:自定义插件停用后,您的自定义帖子类型将不再可见。 虽然,这些自定义帖子类型的数据仍然存在,但是您将无法访问您的自定义帖子类型,因为它将被取消注册。 在那种情况下,手动创建自定义帖子类型将被证明是可行的选择。

    为了创建自定义帖子类型,我们需要使用register_post_type()函数注册我们的自定义帖子类型。 例如,让我们创建一个代码,将“电影”注册为我们的自定义帖子类型。 此代码应添加到function.php文件中:

    // Our custom post type function
    function create_posttype() {
    register_post_type( 'movies',
    // CPT Options
    array(
    'labels' => array(
    'name' => __( 'Movies' ),
    'singular_name' => __( 'Movie' )
    ),
    'public' => true,
    'has_archive' => true,
    'rewrite' => array('slug' => 'movies'),
    );
    }
    
    // Hooking up our function to theme setup
    add_action( 'init', 'create_posttype' );

    上面的代码将创建一个帖子类型“电影”。 接下来,我们定义了一个带有一些参数的数组,这些参数用作自定义帖子类型的选项。 如您所见,该数组分为两个不同的部分。 第一部分包含一些标签,这些标签以两种形式定义自定义帖子类型名称:单数和复数。 第二部分包含其他参数,其中包括将在您的帖子类型URL中使用的公共可见性,has_archive和slug。

    您可以选择向自定义帖子类型添加更多选项,以增强其功能。

    如何在您的网站上显示自定义帖子类型?

    众所周知,WordPress提供了对不同功能的内置支持。 同样,它甚至提供了显示自定义帖子类型的支持。 您可以通过多种方式显示帖子类型。 在我们的案例中,我们将讨论如何在首页上展示它们。 为此,在主题的functions.php文件中添加以下代码:

Wrapping Up!

结语!

Do you wish to improve your post type capabilities or simply want to add some new ones? You can create custom post types to meet such objectives. There is a lot more you can do with custom post types. Hopefully, this post will help you understand how you can create your own custom post type in the most effective and efficient manner.

您是否希望提高帖子类型功能或只是想添加一些新功能? 您可以创建自定义帖子类型来实现这些目标。 您可以使用自定义帖子类型做更多的事情。 希望这篇文章可以帮助您了解如何以最有效和高效的方式创建自己的自定义帖子类型。

About the Author: Edward Jones is a developer who keeps sharing his experience through his insightful blogs. If you need to hire professional wordpress programmer then simply get in touch with Edward via Twitter and Google+.

作者简介 :Edward Jones是一位开发人员,他通过自己有见地的博客不断分享自己的经验。 如果您需要聘请专业的wordpress程序员,则只需通过Twitter和Google+与Edward取得联系。

翻译自: https://www.journaldev.com/6796/wordpress-custom-post-types-what-is-it-and-how-it-can-be-created

wordpress 自定义

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值