如何在WordPress中更改自定义帖子类型永久链接

Recently, one of our readers asked if it was possible to change the custom post type permalink structure in WordPress. By default, you can change the permalink structure of WordPress posts, but not custom post types. In this article, we will show you how to change custom post type permalinks in WordPress.

最近,我们的一位读者询问是否可以更改WordPress中的自定义帖子类型永久链接结构。 默认情况下,您可以更改WordPress帖子的永久链接结构,但不能更改自定义帖子类型。 在本文中,我们将向您展示如何在WordPress中更改自定义帖子类型的永久链接。

How to change custom post type permalinks in WordPress
为什么要更改WordPress中的自定义帖子类型永久链接? (Why Change Custom Post Type Permalinks in WordPress?)

WordPress uses SEO friendly URL structure which makes your WordPress URLs readable and search engine friendly. These SEO friendly URLs are called permalinks.

WordPress使用SEO友好的URL结构 ,使您的WordPress URL可读且对搜索引擎友好。 这些SEO友好的URL称为永久链接。

You can customize permalinks by visiting Settings » Permalinks page. However, you will notice that you can only change permalink structure for blog posts.

您可以通过访问设置»永久链接页面来自定义永久链接 。 但是,您会注意到,您只能更改博客文章的永久链接结构。

Permalink settings page in WordPress

If you are using custom post types or custom taxonomies on your website, then WordPress will use default permalink structure for those.

如果您在网站上使用自定义帖子类型自定义分类法 ,则WordPress将使用默认的永久链接结构。

For example, if you have a custom post type called ‘Movies’, then its URL structure will look like this:

例如,如果您有一个名为“电影”的自定义帖子类型,则其URL结构将如下所示:

http://example.com/movies/the-force-awakens/

http://example.com/movies/the-force-awakens/

If your custom post type has archives enabled, then the archive page URL will look like this:

如果您的自定义帖子类型启用了存档,则存档页面URL将如下所示:

http://example.com/movies/

http://example.com/movies/

This URL scheme is quite SEO friendly and in most cases you don’t need to change it. However, sometimes you may want to customize it to meet your own needs.

该URL方案非常适合SEO,在大多数情况下,您无需进行更改。 但是,有时您可能需要对其进行自定义以满足自己的需求。

Having said that, let’s see how you can easily customize custom post type permalink structure in WordPress.

话虽如此,让我们看看如何在WordPress中轻松自定义自定义帖子类型的永久链接结构。

在WordPress中更改自定义帖子类型永久链接 (Changing Custom Post Type Permalinks in WordPress)

First thing you need to do is install and activate the Custom Post Type Permalinks plugin. For more details, see our step by step guide on how to install a WordPress plugin.

您需要做的第一件事是安装并激活Custom Post Type Permalinks插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Settings » Permalinks page and scroll down to ‘Permalink Settings for Custom Post Types’ section.

激活后,您需要访问设置»永久链接页面,然后向下滚动到“自定义帖子类型的永久链接设置”部分。

Custom post types permalinks

Here you can change the permalink structure of single items in your custom post type.

在这里,您可以更改自定义帖子类型中单个项目的永久链接结构。

You can use all the standard WordPress permalink tags. For a complete list of available tags, see our article on using SEO friendly URL structure in WordPress.

您可以使用所有标准的WordPress永久链接标记。 有关可用标签的完整列表,请参阅有关在WordPress中使用SEO友好URL结构的文章。

Let’s assume that you want to use post ID instead of post name in the URL. In that case all you need to do is add this tag:

假设您要在URL中使用帖子ID代替帖子名称。 在这种情况下,您需要做的就是添加以下标记:

/%post_id%/

/%post_id%/

Notice the trailing slashes at the beginning and end of the tag. These trailing slashes are the separators, and without them your permalinks will not work.

请注意,标记开头和结尾处的斜杠。 这些结尾的斜杠是分隔符,没有它们,您的永久链接将无法工作。

Adding tags to customize custom post type permalinks

Don’t forget to click on the save changes button to store your settings.

不要忘记单击“保存更改”按钮来存储您的设置。

Now the permalinks for a single item in your custom post type will look like this:

现在,您的自定义帖子类型中单个项目的永久链接将如下所示:

http://example.com/movies/133/

http://example.com/movies/133/

The number in the URL will be the numeric ID of the post in your WordPress database.

URL中的数字将是WordPress数据库中帖子数字ID

Let’s take a look at another example.

让我们看另一个例子。

Let’s say you want to add /year/ before the post name in URL. In that case, you will use these tags:

假设您要在URL中的帖子名称之前添加/ year /。 在这种情况下,您将使用以下标记:

/%year%/%post_name%/

/%year%/%post_name%/

Click on the save changes button to update your permalinks.

单击保存更改按钮以更新您的永久链接。

Now the single item permalink for your custom post type will look like this:

现在,您的自定义帖子类型的单个项目永久链接将如下所示:

http://example.com/movies/2016/the-force-awakens/

http://example.com/movies/2016/the-force-awakens/

So far so good. Now let’s try another example.

到目前为止,一切都很好。 现在让我们尝试另一个示例。

Let’s assume that you are using a custom taxonomy called ‘genre’ with your custom post type ‘movies’ and you want to add the custom taxonomy term into the URL. In that case, you will use these tags:

假设您正在将自定义分类法称为“流派”,并将其自定义帖子类型为“电影”,并且要将自定义分类法术语添加到URL中。 在这种情况下,您将使用以下标记:

/%genre%/%post_name%/

/%genre%/%post_name%/

In this example, genre is the slug of our custom taxonomy.

在这个例子中,流派是蛞蝓我们的自定义分类的。

Click on the save changes button to update your permalinks.

单击保存更改按钮以更新您的永久链接。

Now the single item permalink for your custom post type will look like this:

现在,您的自定义帖子类型的单个项目永久链接将如下所示:

http://example.com/movies/sci-fi/the-force-awakens/

http://example.com/movies/sci-fi/the-force-awakens/

That’s all, we hope this article helped you learn how to easily change custom post type permalinks in WordPress. You may also want to see our list of 12 most useful WordPress custom post types tutorials.

仅此而已,我们希望本文能帮助您学习如何轻松地在WordPress中更改自定义帖子类型的永久链接。 您可能还想查看我们的12个最有用的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 Facebook.

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

翻译自: https://www.wpbeginner.com/plugins/how-to-change-custom-post-type-permalinks-in-wordpress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值