如何在WordPress中创建自定义帖子类型存档页面

Custom Post Types was one of the awesome features included in WordPress 3.0. One of our users asked us a question on twitter, how to create a custom post types archive page. We covered it in our initial article about Custom Post Types, but it wasn’t explained thoroughly. In this article, we will show you a step by step guide on how to create a custom post types archive page in WordPress.

自定义帖子类型WordPress 3.0中包含的出色功能之一 。 我们的一位用户在Twitter上向我们提出了一个问题,即如何创建自定义帖子类型存档页面。 我们在有关自定义帖子类型的初始文章中对此进行了介绍,但是并没有对其进行详尽的解释。 在本文中,我们将向您逐步介绍如何在WordPress中创建自定义帖子类型存档页面。

First thing you need to make sure is that your custom post type has archive enabled in the code. To do this, you need to go to your custom post type code (can be found in your theme’s functions.php file or site-specific plugin file). You need to make sure that you have has_archive argument set to be true.

您需要确保的第一件事是您的自定义帖子类型已在代码中启用了存档。 为此,您需要转到自定义帖子类型代码(可以在主题的functions.php文件或特定站点的插件文件中找到)。 您需要确保has_archive参数设置为true。

An example code would look like this:

示例代码如下所示:


add_action( 'init', 'create_post_type' );
function create_post_type() {
	register_post_type( 'deals',
		array(
			'labels' => array(
				'name' => __( 'Deals' ),
				'singular_name' => __( 'Deal' )
			),
		'public' => true,
		'has_archive' => true,
		)
	);
}

Notice how we have has_archive set to be true in the array. You need to have this in your code. Once you do that, your custom post type will be assigned an archive page which can be accessed by going to a link like this:

注意我们如何在数组中将has_archive设置为true。 您需要在代码中包含它。 完成后,将为您的自定义帖子类型分配一个存档页面,可以通过以下链接访问该页面:

http://yoursite.com/deals/

http://yoursite.com/deals/

Note: replace deals with whatever is the name of your custom post type.

注意:请使用自定义帖子类型的名称替换交易。

WordPress by default uses the archive template of your theme to display the custom post type archive page. If you are ok with how the default archive looks, then you are done here. You have successfully created a custom post type archive page for your WordPress site.

默认情况下,WordPress使用主题的存档模板显示自定义帖子类型存档页面。 如果您对默认存档的外观没意见,那么您就可以在这里完成。 您已成功为WordPress网站创建了自定义帖子类型存档页面。

However, if you want to create a custom archive page for your custom post type, then you would need to create a new file called archive-{posttype}.php. In our example, the file name would be archive-deals.php

但是,如果要为自定义帖子类型创建自定义存档页面,则需要创建一个名为archive- {posttype} .php的新文件。 在我们的示例中,文件名将为archive-deals.php

The best way to start would be to copy the code from your theme’s archive.php file and paste it in your archive-{posttype}.php file. Then start tweaking from there. You can style this archive file to your heart’s desire. A very basic template would look like this:

最好的开始方法是从主题的archive.php文件中复制代码并将其粘贴到archive- {posttype} .php文件中。 然后从那里开始调整。 您可以根据自己的意愿设置此存档文件的样式。 一个非常基本的模板如下所示:


<?php
get_header();
if(have_posts()) : while(have_posts()) : the_post();
	the_title();
	echo '<div class="entry-content">';
	the_content();
	echo '</div>';
endwhile; endif;
get_footer();
?>

Once you are done upload this file in your theme’s directory. After you have done that, you would see a custom archive page for your custom post type.

完成后,将此文件上传到主题目录中。 完成此操作后,您将看到自定义帖子类型的自定义存档页面。

There you have it. You should now have a custom post types archive page in WordPress. For those who want to see custom examples, then you can see those by visiting our WordPress coupons page or our WordPress books page.

你有它。 您现在应该在WordPress中有一个自定义帖子类型存档页面。 对于那些想查看自定义示例的人,您可以通过访问我们的WordPress优惠券页面WordPress图书页面来查看那些示例。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-create-a-custom-post-types-archive-page-in-wordpress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值