12种最有用的WordPress自定义帖子类型教程

WordPress allows you to create your own custom content types. These content types are called custom post types. You can use them to add custom content like products, reviews, recipes, etc. In this article, we will share the 12 most useful WordPress custom post types tutorials.

WordPress允许您创建自己的自定义内容类型。 这些内容类型称为自定义帖子类型 。 您可以使用它们来添加自定义内容,例如产品,评论,食谱等。在本文中,我们将分享12个最有用的WordPress自定义帖子类型教程。

Custom Post Types
影片教学 (Video Tutorial)

演示地址

If you don’t like the video or need more instructions, then continue reading.

如果您不喜欢该视频或需要更多说明,请继续阅读。

1.什么时候需要自定义帖子类型 (1. When Do You Need A Custom Post Type)

Before you start creating custom post types or taxonomies on your WordPress site, it’s important to evaluate your needs. A lot of times you can achieve the same results with the default WordPress posts and pages.

在WordPress网站上开始创建自定义帖子类型或分类法之前,评估您的需求很重要。 很多时候,您可以使用默认的WordPress 帖子和页面获得相同的结果。

With the help of built-in categories and tags, you can sort your content in many different ways. With pages you can set up a hierarchical layout of content with parent-child relationship.

借助内置的类别和标签 ,您可以用许多不同的方式对内容进行排序。 使用页面,您可以设置具有父子关系的内容的分层布局。

If you are unclear, then refer to this guide about when do you need a custom post type or taxonomy in WordPress.

如果不清楚,请参阅本指南,了解何时需要WordPress中的自定义帖子类型或分类法

2.添加自定义帖子类型的图标 (2. Adding Icon for Custom Post Types)

When you create a custom post type, WordPress uses the default post icon if no icon is provided. You can add your own icons to custom post types. These icons are displayed in your WordPress admin bar.

创建自定义帖子类型时,如果未提供图标,则WordPress将使用默认的帖子图标。 您可以将自己的图标添加到自定义帖子类型。 这些图标显示在您的WordPress管理栏中。

WordPress uses font icons in the admin area to ensure they look beautiful on all devices and screen sizes. The easiest way to add a font icon to your custom post type is by using the CPT Custom Icon plugin. Simply install and activate the plugin and visit the plugin’s settings page to add an icon.

WordPress在管理区域中使用字体图标,以确保它们在所有设备和屏幕尺寸上均美观。 将字体图标添加到自定义帖子类型的最简单方法是使用CPT自定义图标插件。 只需安装并激活插件,然后访问插件的设置页面即可添加图标。

Adding a custom post type icon

Another method is to add the icon while you are creating your Custom Post Type with CPT UI plugin.

另一种方法是在使用CPT UI插件创建自定义帖子类型时添加图标。

For more detailed instructions, see this tutorial on how to add icons for custom post types in WordPress.

有关更多详细说明,请参阅本教程,了解如何在WordPress中为自定义帖子类型添加图标

3.创建自定义帖子类型存档页面 (3. Creating a Custom Post Types Archive Page)

Adding custom post types in WordPress has become very easy thanks to the CPT UI plugin. However, many beginners have trouble displaying their custom post types on their websites.

多亏了CPT UI插件,在WordPress中添加自定义帖子类型变得非常容易。 但是,许多初学者很难在其网站上显示其自定义帖子类型。

To add custom post type archive page, first you need to make sure that archives are enabled for your custom post type. In most cases, they are but if not then you will need to enable them. CPT UI plugin has a checkbox under advanced options to enable archives.

要添加自定义帖子类型档案页面,首先需要确保为自定义帖子类型启用档案。 在大多数情况下,它们是,但如果不是,则需要启用它们。 CPT UI插件在高级选项下具有一个复选框,以启用存档。

Enable Archive for CPT

Now if you are using pretty permalinks, then you can access custom post type archives page by visiting a URL like this:

现在,如果您使用漂亮的永久链接,则可以通过访问如下URL来访问自定义帖子类型档案页面:

http://www.example.com/products/

http://www.example.com/products/

Replace example.com with your own domain name and products with your custom post type name. If you cannot see your custom post type, then visit Settings » Permalinks and click on the save changes button. This will update your permalink structure, and you should now be able to see your custom post type archive page.

用您自己的域名替换example.com,用您的自定义帖子类型名称替换产品。 如果看不到自定义帖子类型,请访问设置»永久链接 ,然后单击保存更改按钮。 这将更新您的永久链接结构,现在您应该可以看到自定义帖子类型存档页面。

For more detailed instructions, check out this guide on custom post types archives page.

有关更多详细说明,请在自定义帖子类型档案页面上查看本指南。

You can also add a menu item to your custom post type in navigation menus. Simply go to Appearance » Menus and click on the ‘Custom Link’ tab. Next, add your custom post type name and URL.

您还可以将菜单项添加到导航菜单中的自定义帖子类型。 只需转到外观»菜单 ,然后单击“自定义链接”标签。 接下来,添加您的自定义帖子类型名称和URL。

Adding custom post type archive page to navigation menu in WordPress
4.自定义帖子类型的RSS提要 (4. RSS Feeds for Custom Post Types)

WordPress comes with a built-in RSS generator for all content types, taxonomies, authors, and date based archives. It uses a proper URL structure that queries the database and generates the RSS feed you want to see.

WordPress带有一个内置的RSS生成器,用于所有内容类型,分类法,作者和基于日期的存档。 它使用适当的URL结构来查询数据库并生成您要查看的RSS feed。

For example, to see the RSS feed of your custom post type ‘movies’ you will add this URL:

例如,要查看您的自定义帖子类型“电影”的RSS提要,您将添加以下URL:

http://www.example.com/feed/?post_type=movies

http://www.example.com/feed/?post_type=movies

For more details, see how to make separate RSS feed for custom post types in WordPress.

有关更多详细信息,请参阅如何为WordPress中的自定义帖子类型制作单独的RSS feed

5.将自定义帖子类型添加到您的主要RSS Feed (5. Adding Custom Post Types to Your Main RSS Feed)

By default, WordPress only includes your blog posts in your site’s main RSS feed. This means that your custom post type content is not visible to your main RSS feed subscribers.

默认情况下,WordPress仅将您的博客文章包含在站点的主要RSS feed中。 这意味着您的自定义帖子类型内容对您的主要RSS feed订阅者不可见。

You can easily include your custom post types into your main RSS feed by adding the following code to your theme’s functions.php file or a site-specific WordPress plugin.

通过将以下代码添加到主题的functions.php文件或特定于站点的WordPress插件中,您可以轻松地将自定义帖子类型包括到主RSS feed中。


function myfeed_request($qv) {
	if (isset($qv['feed']) && !isset($qv['post_type']))
		$qv['post_type'] = array('post', 'books', 'movies');
	return $qv;
}
add_filter('request', 'myfeed_request');

Replace books and movies with your own custom post types.

用您自己的自定义帖子类型替换书籍和电影。

Learn more about how to add custom post types to main WordPress RSS feed.

了解有关如何将自定义帖子类型添加到主要WordPress RSS feed的更多信息

6.搜索表单以查找自定义帖子类型 (6. Search Form for Custom Post Types)

Advanced Search Form

The default WordPress search feature is very limited. Specially if you are using custom post types on your WordPress site. You can add a custom Google Search form to your WordPress site, but even that form doesn’t allow users to select search parameters.

默认的WordPress搜索功能非常有限。 特别是如果您在WordPress网站上使用自定义帖子类型。 您可以将自定义Google搜索表单添加到WordPress网站,但是即使该表单也不允许用户选择搜索参数。

Another solution is to use a plugin like SearchWP which allows you to create advanced search forms and is capable of searching through all custom post types, taxonomies, and custom fields.

另一个解决方案是使用SearchWP之类的插件,该插件可让您创建高级搜索表单,并能够搜索所有自定义帖子类型,分类法和自定义字段。

If you would rather prefer to build your own custom search form, then here is what you need to do. First add your custom post types hidden fields in the search form by adding this code in searchform.php file of your child theme.

如果您希望构建自己的自定义搜索表单 ,那么这就是您需要做的。 首先,通过将此代码添加到您的子主题的searchform.php文件中,在搜索表单中添加您的自定义帖子类型隐藏字段。


<input type="hidden" name="post_type[]" value="articles" />
<input type="hidden" name="post_type[]" value="post" />
<input type="hidden" name="post_type[]" value="videos" /> 
<input type="hidden" name="post_type[]" value="books" />  

This code simply adds hidden fields for your custom post types, replace value with your own custom post types. The next step is to tell WordPress what to do with these fields. Add this code before the loop in your theme’s search.php file:

此代码只是为您的自定义帖子类型添加隐藏字段,将值替换为您自己的自定义帖子类型。 下一步是告诉WordPress如何处理这些字段。 在主题的search.php文件中的循环之前添加以下代码:


<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">
<input type="text" name="s" id="s" <?php if(is_search()) { ?>value="<?php the_search_query(); ?>" <?php } else { ?>value="Enter keywords &hellip;" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"<?php } ?> /><br />
	
<?php $query_types = get_query_var('post_type'); ?>
    
<input type="checkbox" name="post_type[]" value="articles" <?php if (in_array('articles', $query_types)) { echo 'checked="checked"'; } ?> /><label>Articles</label>
<input type="checkbox" name="post_type[]" value="post" <?php if (in_array('post', $query_types)) { echo 'checked="checked"'; } ?> /><label>Blog</label>
<input type="checkbox" name="post_type[]" value="books" <?php if (in_array('books', $query_types)) { echo 'checked="checked"'; } ?> /><label>Books</label>
<input type="checkbox" name="post_type[]" value="videos" <?php if (in_array('videos', $query_types)) { echo 'checked="checked"'; } ?> /><label>Videos</label>
    
<input type="submit" id="searchsubmit" value="Search" />
</form>

7.为自定义帖子类型添加粘性帖子 (7. Adding Sticky Posts for Custom Post Types)

Sticky posts is a feature in WordPress which allows you to add featured posts. By default it only works for blog posts.

便利贴是WordPress中的一项功能,可让您添加特色帖子。 默认情况下,它仅适用于博客文章。

You can enable it for your custom post types as well by installing and activating the Sticky Custom Post Types plugin. Upon activation, go to Settings » Reading and scroll down to the section Sticky Custom Post Types.

您还可以通过安装和激活“ 粘性自定义帖子类型”插件为您的自定义帖子类型启用它。 激活后,转到“设置”»“阅读”,然后向下滚动到“粘性自定义帖子类型”部分。

Next, you need to choose the custom post types where you want Stick This option to be enabled.

接下来,您需要选择要启用此选项的自定义帖子类型。

For more details see this tutorial on how to add sticky posts for custom post types in WordPress.

有关更多详细信息,请参阅本教程,了解如何在WordPress中为自定义帖子类型添加粘性帖子

8.在自定义帖子类型上禁用Disqus (8. Disable Disqus on Custom Post Types)

If you are using Disqus commenting system in WordPress, then you will notice that your Disqus comment box will start appearing on all your custom post types. If you do not want users to comment on these custom post types, then you will need to manually disable Disqus for selective post types. Simply add this code snippet into your theme’s functions.php file or a site-specific plugin.

如果您正在WordPress中使用Disqus评论系统 ,那么您会注意到,您的Disqus评论框将开始出现在所有自定义帖子类型上。 如果您不希望用户在这些自定义帖子类型上发表评论,那么您将需要为某些选择性的帖子类型手动禁用Disqus。 只需将此代码段添加到主题的functions.php文件或特定于站点的插件中即可。


add_filter( 'comments_template' , 'wpb_block_disqus', 1 );
function wpb_block_disqus($file) {
if ( 'custom_post_type_name' == get_post_type() )
remove_filter('comments_template', 'dsq_comments_template');
return $file;

}

Don’t forget to replace custom_post_type_name with your own custom post type name. For more detailed instructions see our guide on how to disable Disqus on custom post types in WordPress.

不要忘记用您自己的自定义帖子类型名称替换custom_post_type_name。 有关更多详细说明,请参阅我们的指南,了解如何在WordPress中自定义帖子类型上禁用Disqus

9.以自定义帖子类型添加用户提交的内容 (9. Adding User Submitted Content in Custom Post Types)

Sometimes you may want to allow users to submit content for custom post types on your site. For example, movie reviews, recipes, etc.

有时,您可能希望允许用户在您的网站上提交自定义帖子类型的内容。 例如,电影评论,食谱等。

One way to do this is by giving users access to your WordPress admin area and assigning them author user role. But if you don’t want that, then there is another way.

一种方法是让用户访问您的WordPress管理区域,并为他们分配作者用户角色。 但是,如果您不希望这样做,那么还有另一种方法。

You will need Gravity Forms plugin for that. It enables you to add a public content submission form that your website visitors can use.

为此,您将需要Gravity Forms插件。 它使您可以添加网站访问者可以使用的公共内容提交表单。

After installing Gravity Forms, you will need to install and activate Gravity Forms + Custom Post Types plugin as well.

安装Gravity Forms之后 ,您还需要安装并激活Gravity Forms + Custom Post Types插件。

Now create a new form, and you will notice that there will be an option to map each field to your custom post types. For more detailed instructions see our tutorial on how to add user submitted content for custom post types in WordPress.

现在创建一个新表单,您会注意到有一个选项可以将每个字段映射到您的自定义帖子类型。 有关更多详细说明,请参阅我们的教程,该教程如何在WordPress中为自定义帖子类型添加用户提交的内容

10.切换或转换自定义帖子类型 (10. Switch or Convert Custom Post Types)

You may come across situations where you will have to merge or convert custom post types. You may also want to move items from one post type to another.

您可能会遇到必须合并或转换自定义帖子类型的情况。 您可能还希望将项目从一种帖子类型移动到另一种。

Simply install and activate the Post Type Switcher plugin. Upon activation, go to Posts » All Posts. If it is a custom post type, then go to the screen that shows all items in that post type.

只需安装并激活Post Type Switcher插件即可。 激活后,转到帖子»所有帖子 。 如果是自定义帖子类型,则转到显示该帖子类型中所有项目的屏幕。

Select the items or posts that you want to move, then click on quick edit link. You will notice a new drop down menu allowing you to change post type for that item.

选择您要移动的项目或帖子,然后单击快速编辑链接。 您会注意到一个新的下拉菜单,可用于更改该项目的帖子类型。

Post Type Switcher

For more detailed instructions see our tutorial on how to switch or convert custom post types in WordPress.

有关更多详细说明,请参阅有关如何在WordPress中切换或转换自定义帖子类型的教程。

11.使用自定义帖子类型作为分类法 (11. Using Custom Post Types as Taxonomies)

Yes, you read that correctly, and you are probably thinking why use CPTs as taxonomies? Why not just create custom taxonomies? Let’s say that you have a custom post type for books and another custom post type for authors. Now you may want to associate authors with the books they have written. You can create a custom taxonomy for authors, but then you will have an authors taxonomy and a post type which only adds to the confusion.

是的,您没看错,您可能在想为什么使用CPT作为分类法? 为什么不只创建自定义分类法呢? 假设您有一个针对书籍的自定义帖子类型,以及一个针对作者的自定义帖子类型。 现在,您可能需要将作者与他们所写的书联系起来。 您可以为作者创建自定义分类法,但是您将拥有一个作者分类法和一个帖子类型,这只会增加混乱。

Simply install and activate the CPT-onomies plugin. It allows you to build relationships between post types by using custom post types as taxonomy.

只需安装并激活CPT-onomies插件即可。 它允许您通过使用自定义帖子类型作为分类法在帖子类型之间建立关系。

Using this plugin, the next time you add a book you can select the author as CPT-onomy and show all the books written by that author. Learn more about using custom post types as taxonomies.

使用此插件,下次添加书时,可以选择作者作为CPT-onomy,并显示该作者写的所有书。 了解有关使用自定义帖子类型作为分类法的更多信息。

12.帖子类型的自定义元框 (12. Custom Meta Boxes for Post Types)

Custom meta boxes allow you to add custom fields into WordPress post editor screen. This way you can create additional input fields for your post types. The easiest way to create custom meta fields in WordPress is by using the Advanced Custom Fields plugin.

自定义元框允许您将自定义字段添加到WordPress帖子编辑器屏幕中。 这样,您可以为帖子类型创建其他输入字段。 在WordPress中创建自定义元字段的最简单方法是使用“ 高级自定义字段”插件。

After installing the plugin, simply click on Custom Fields in WordPress admin bar and then click on Add New. Follow on screen instructions to create a group of custom fields you want to add to your custom post type.

安装插件后,只需单击WordPress管理栏中的“ 自定义字段” ,然后单击“添加新项”。 按照屏幕上的说明创建一组要添加到自定义帖子类型的自定义字段。

Creating custom fields for post types

This is how the custom fields panel will appear on the post edit screen for your post type:

这是您的帖子类型的自定义字段面板将显示在帖子编辑屏幕上的方式:

custom fields as displayed on a custom post type edit screen

You can display data entered into your custom fields by modifying your theme templates. For example, we would use <?php the_field('imdb_url'); ?> template tag to display IMDB URL custom field.

您可以通过修改主题模板来显示输入到自定义字段中的数据。 例如,我们将使用<?php the_field('imdb_url'); ?> <?php the_field('imdb_url'); ?>模板标记以显示IMDB URL自定义字段。

You can also use shortcode like [acf field="{$imdb_url}"] in your post types to display custom field data.

您还可以在帖子类型中使用[acf field="{$imdb_url}"]类的[acf field="{$imdb_url}"]来显示自定义字段数据。

That’s all we hope this article helped you learn some cool tricks for custom post types in WordPress. You may also want to take a look at these best category hacks and plugins for WordPress.

这就是我们希望本文帮助您了解WordPress中自定义帖子类型的一些绝妙技巧。 您可能还想看看这些针对WordPress的最佳类别黑客和插件

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在TwitterGoogle+上找到我们。

翻译自: https://www.wpbeginner.com/wp-tutorials/12-most-useful-wordpress-custom-post-types-tutorials/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值