最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Should the actual category directory be 404? Is that normal WP behaviour

matteradmin4PV0评论

I'm not sure if this is 'normal' but on my WP install the actual directory for 'category' throws a 404 error.

So, for example, if I had a category called 'apples' then every post or page associated with the 'apples' category has this URL string:

my-site/category/apples/ 

The above for me loads fine.

However, if I remove the 'apples' from the URL then a 404 error is generated, which I am sure is correct, or should there be an index page or other?

The reason I am asking about this is because I created some custom Taxonomy called 'US States' which works like this:

my-site/us-states/florida < loads great with archive

my-site/us-states/ < Error 404

Is there anyway to make the HOME of the actual taxonomy a templated page or other?

Thanks

I'm not sure if this is 'normal' but on my WP install the actual directory for 'category' throws a 404 error.

So, for example, if I had a category called 'apples' then every post or page associated with the 'apples' category has this URL string:

my-site/category/apples/ 

The above for me loads fine.

However, if I remove the 'apples' from the URL then a 404 error is generated, which I am sure is correct, or should there be an index page or other?

The reason I am asking about this is because I created some custom Taxonomy called 'US States' which works like this:

my-site/us-states/florida < loads great with archive

my-site/us-states/ < Error 404

Is there anyway to make the HOME of the actual taxonomy a templated page or other?

Thanks

Share Improve this question edited Apr 8, 2019 at 5:22 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 8, 2019 at 4:47 HenryHenry 9831 gold badge8 silver badges31 bronze badges 1
  • 2 This is the standard behaviour, yes. By default all URLs in WordPress are lists of posts or single posts. There are no posts that it would make sense to display at such a URL. – Jacob Peattie Commented Apr 8, 2019 at 5:02
Add a comment  | 

2 Answers 2

Reset to default 2

Yes, it's normal behavior. WordPress uses a set of rewrite rules to process requests. Here are the rules that will match requests related to categories:

[category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[2]
[category/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[2]
[category/(.+?)/page/?([0-9]{1,})/?$] => index.php?category_name=$matches[1]&paged=$matches[2]
[category/(.+?)/?$] => index.php?category_name=$matches[1]

As you can see, all of them require, that the request is longer than /category/.

And it makes sense.

  • category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/ - will match the feed for category
  • [category/(.+?)/(feed|rdf|rss|rss2|atom)/ - is a shorter URL for feed
  • category/(.+?)/page/?([0-9]{1,})/ - is support for pagination
  • category/(.+?)/ - is just category listing (first page)

So all of the URLs above are related to given term in this taxonomy and you know which term, because it is defined in URL.

On the other hand, what should be displayed when you go to /category/? No term is defined, so you can't select any one of them. So should it show you all posts on your site? (Blog index already does it).

Sometimes it makes sense to show the list of categories on such URL, but it isn't a common practice.

You can always add your custom rewrite rule to process such requests.

You can create template files for each level of the category tree. Also for the one you mentioned. If you create a template called category.php or archive.php WordPress will load it on this level. Place it in your themes root folder.

Find more detailed info on the view hierarchy here: https://developer.wordpress/themes/basics/template-hierarchy/#examples

Post a comment

comment list (0)

  1. No comments so far