Wordpress - 在子类别存档 URL 中显示父类别和子类别名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18538265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 19:20:16  来源:igfitidea点击:

Wordpress - Show parent category AND subcategory name in subcategory archive URL

wordpresswordpress-pluginwordpress-theming

提问by rossautomatica

I'm having some trouble with Wordpress category/subcategoryarchive URLs.

我在使用 Wordpresscategory/subcategory存档 URL 时遇到了一些问题。

For example, I want to be able to display this archive:
http://faroutmagazine.co.uk/wp/track-of-the-daywhile keeping its parent category in the URL, making it http://faroutmagazine.co.uk/wp/music/track-of-the-day

例如,我希望能够显示此存档:
http://faroutmagazine.co.uk/wp/track-of-the-day同时将其父类别保留在 URL 中,使其http://faroutmagazine.co.uk/wp/music/track-of-the-day

Previously, I was just getting a "This is embarrassing..." 404 message when accessing the /music/track-of-the-dayURL (even though that's the one that was appearing in the View option under categories in the admin section of WP).

以前,我在访问/music/track-of-the-dayURL时只是收到“这太令人尴尬了...”404 消息(即使那是出现在 WP 管理部分类别下的“查看”选项中的消息)。

Now the View link in the admin area only shows me the /track-of-the-dayURL, and this DOES display the category posts which is great, but I want it to include the parent category in the URL.

现在管理区域中的查看链接只向我显示/track-of-the-dayURL,这确实显示了很棒的类别帖子,但我希望它在 URL 中包含父类别。

Is there anything I can do to the functions.phpfile or any plugins I can add to make this happen? Please bear in mind that there are multiple subcategory archives on this site, and they should all contain the parent category in the URL.

我可以对functions.php文件做些什么或我可以添加任何插件来实现这一点吗?请记住,此站点上有多个子类别存档,它们都应在 URL 中包含父类别。

***UPDATE: I actually have just set everything up a different way now using Pages and showing category archives on pages as it's taking up too much time. Thanks for your answer though. People telling me to use %category%/%postname%as the permalink structure are incorrect - yes, this works for the POST urls but not for the archive category of the subcategory which is the problem I was actually looking for help with. If anyone can provide an answer, it would be appreciated, but I am using this workaround now as this was taking up way too much time trying to figure out.

***更新:实际上,我现在使用 Pages 以不同的方式设置所有内容并在页面上显示类别档案,因为它占用了太多时间。不过还是谢谢你的回答。人们告诉我将其%category%/%postname%用作永久链接结构是不正确的 - 是的,这适用于 POST url,但不适用于子类别的存档类别,这是我实际上正在寻求帮助的问题。如果有人能提供答案,我们将不胜感激,但我现在正在使用这种解决方法,因为这花费了太多时间试图弄清楚。

回答by Peter

This happens because you chose category and subcategory from the right options tick boxes when editing the entry. Choose only the subcategory box in every entry and it will show the nested URL.

发生这种情况是因为您在编辑条目时从正确的选项复选框中选择了类别和子类别。在每个条目中只选择子类别框,它将显示嵌套的 URL。

回答by Maruti Mohanty

To make this happen you need to make some change in permalinks

要实现这一点,您需要对永久链接进行一些更改

go to Settings->Permalinksthen select custom structure and use this syntax " /%category%/%postname%/"in the field

转到设置->固定链接,然后选择自定义结构并在字段中使用此语法“ /%category%/%postname%/

Now save the settings and you can see the post url's using its category name in the permalink

现在保存设置,您可以在永久链接中使用其类别名称查看帖子网址

Hope it helps :)

希望能帮助到你 :)

回答by user2655393

Actually hierarchical subcategory archive permalinks are default in wordpress.

实际上分层子类别存档永久链接在 wordpress 中是默认的。

If you have a "pretty permalinks" structure selected in settings > permalinks, like /%postname%for example, then category and subcategory archive urls should default to something like: yourdomain.com/category-base/category/sub-category/sub-sub-category

如果您在设置 > 永久链接中选择了“漂亮的永久链接”结构/%postname%,例如,则类别和子类别存档 url 应默认为以下内容: yourdomain.com/category-base/category/sub-category/sub-sub-category

I had encountered a similar issue with a recent WP project, BUT I was using a plugin called WP No Base Permalinkin order to get rid of my category and tag base slugs in my archive permalinks. When I deactivated the plugin, category hierarchy suddenly returned to my archive permalinks (luckily this project was still in development, otherwise, I would have had some major 404 problems).

我在最近的一个 WP 项目中遇到了类似的问题,但我使用了一个名为WP No Base Permalink的插件,以便在我的存档永久链接中摆脱我的类别和标签基础 slug。当我停用插件时,类别层次结构突然返回到我的存档永久链接(幸运的是这个项目仍在开发中,否则我会遇到一些重大的 404 问题)。

I have not yet found/tested another plugin for eliminating category/tag base slugs that will also preserve hierarchy in sub/category archive urls, but it turns out that I do not need that functionality for my current project.

我还没有找到/测试另一个插件来消除类别/标签基础 slug,它也将保留子/类别存档 URL 中的层次结构,但事实证明,我当前的项目不需要该功能。

Instead I am using the base slugs blog-articlesand blog-tagsfor the standard categories and tags in order to set the blog taxonomies apart from other taxonomies. Then I am using the plugin Typesto create a custom post type and custom taxonomies for that post type. The Types plugin has an option in the advanced settings to set hierarchical taxonomy urls to trueor falsewhen creating a custom taxonomy.

相反,我使用基本 slugblog-articlesblog-tags标准类别和标签,以便将博客分类法与其他分类法分开。然后我使用插件类型为该帖子类型创建自定义帖子类型和自定义分类法。插件在高级设置中的选项来设置分层分类的URL类型truefalse创建一个自定义分类时。

Finally, I found a plugin called Remove Taxonomy Base Slugthat effectively eliminates the default taxonomy term from the base slug of the taxonomy archive urls. And this, thankfully, does not interfere with the hierarchical sub/category urls, OR the base slugs for standard tags and categories.

最后,我找到了一个名为Remove Taxonomy Base Slug的插件,它有效地从分类法存档 url 的 base slug 中消除了默认分类术语。幸运的是,这不会干扰分层子/类别 url,或标准标签和类别的基本 slug。

So now, my blog categories/tags look like this:

所以现在,我的博客类别/标签如下所示:

yourdomain.com/blog-articles/category/sub-category/or yourdomain.com/blog-tags/tag/

yourdomain.com/blog-articles/category/sub-category/或者 yourdomain.com/blog-tags/tag/

And my custom post type categories/tags look like this:

我的自定义帖子类型类别/标签如下所示:

yourdomain.com/category/sub-category/or yourdomain.com/tag/

yourdomain.com/category/sub-category/或者 yourdomain.com/tag/

Phew!

呼!

I hope this helps!

我希望这有帮助!

回答by Amin Behravesh

You should set hierarchical value to true when you are defining your taxonomy.

在定义分类法时,您应该将层级值设置为 true。

'rewrite' => array('slug' => 'mySlug', 'hierarchical' => true),

'rewrite' => array('slug' => 'mySlug', 'hierarchical' => true),

Then if you have a subcategory the url will be shown like this: http://example.com/taxonomy/parentCategory/subCategory/

然后,如果您有一个子类别,网址将如下所示:http: //example.com/taxonomy/parentCategory/subCategory/

I found this sloution here:

我在这里找到了这个slotion:

https://wordpress.stackexchange.com/questions/155319/how-to-include-parent-terms-in-hierarchical-taxonomy-urls

https://wordpress.stackexchange.com/questions/155319/how-to-include-parent-terms-in-hierarchical-taxonomy-urls

回答by gwc

Setting Settings->Permalinks->Category base to '%category%' should give you what you want. According to WP doc:

将 Settings->Permalinks->Category base 设置为 '%category%' 应该会给你你想要的。根据 WP 文档:

Nested sub-categories appear as nested directories in the URI

嵌套子类别在 URI 中显示为嵌套目录

See http://codex.wordpress.org/Using_Permalinks.

请参阅http://codex.wordpress.org/Using_Permalinks

回答by Shiva

The Permalinks settings should be like /%category%/%postname%/ ..

固定链接设置应该类似于 /%category%/%postname%/ ..

回答by Desh

If use custom permalinks settings as %postname% only, it will make your url in the form of .../category/subcategory.

如果仅将自定义永久链接设置用作 %postname%,它将使您的 url 以 .../category/subcategory 的形式出现。

回答by Manish

You can use WordPress plugin to remove 'category' from url http://wordpress.org/plugins/wp-no-category-base/and then follow the instruction given by Maruti

您可以使用 WordPress 插件从 url http://wordpress.org/plugins/wp-no-category-base/ 中删除“类别” ,然后按照 Maruti 给出的说明进行操作