$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom Post TypeTwo level deep taxonomy (cat. and subcat.) uri problem (error 404)|Programmer puzzle solving
最新消息: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)

Custom Post TypeTwo level deep taxonomy (cat. and subcat.) uri problem (error 404)

matteradmin9PV0评论

I've created a CPT named "decoration products"(productos decoración), the problem concerns to it CPT.

When for the first time I created "decoration products" CPT, I realized that it needed two level taxonomy uri structure. To visually illustrate it:

  • Root_domain/CPT name/ - This is a page that shows all "decoration products" CPT's posts.

  • Root_domain/CPT name/category - This is an archive page that shows top level category's posts.

  • Root_domain/CPT name/category/subcategory - This is an archive page that shows subcategory's posts.

  • Root_domain/CPT name/category/subcategory/post-title - This is a post within a certain top category and subcategory.

I based on this post:

  • wordpress.stackexchange/questions/39500/how-to-create-a-permalink-structure-with-custom-taxonomies-and-custom-post-types

to create two level taxonomy uri structure for "decoration products" CPT. Anyway there were some fixes to do, for instance, double slash in the uri. I fixed it with this helpful post:

  • wordpress.stackexchange/questions/75157/how-to-fix-a-double-slash-in-custom-permalinks-with-hierarchical-taxonomys

I create all my code based on above two posts and put it in functions.php. You can see it in pastebin.

I thought, that everything works well until I’ve realized that there’re some problems with the manner that WordPress handle CPT top-categories and child-categories URIs. These are the problems:

PROBLEM 1:

This interior illumination's post:

Root_domain/productos-decoracion/iluminacion/iluminacion-interior/crea-ambientes-interior-acogedores

When I click on breadcrumb link “Iluminación interior” of this post, it must show “Iluminación interior” archive page, in the following uri:

Root_domain/productos-decoracion/iluminacion/iluminacion-interior

Instead of 404 Page not Found (Error 404: Página no encontrada). If I change manually browser's uri to:

Root_domain/productos-decoracion/iluminacion-interior

404 Page not Found error disappear and everything works well.

If I remove 'hierarchical'=>true from 'rewrite' in function my_taxonomies_product() (you can find it above pastebin function.php code) and I click on breadcrumb link “Iluminación interior”, it shows archive page like I want but in the wrong uri:

Root_domain/productos-decoracion/iluminacion-interior

"Interior illumination" (iluminación interior) is a subcategory of "Illumination" and the uri doesn’t show it, it should be: Root_domain/productos-decoracion/iluminacion/iluminacion-interior

This problems happens with all the other posts that have a top category and a subcategory.

PROBLEM 2:

The second problems comes when I click on breadcrumb link “Productos decoración” of many posts or categories pages, for instance here (Root_domain/productos-decoracion/iluminacion/iluminacion-interior/crea-ambientes-interior-acogedores ). Instead of bring to Root_domain/productos-decoracion (that’s what i want) it adds %field% to the end of the uri. %field% comes from functions.php code included above.

I've tried everything but with no success. I would really appreciate any help to fix this problems.

I've created a CPT named "decoration products"(productos decoración), the problem concerns to it CPT.

When for the first time I created "decoration products" CPT, I realized that it needed two level taxonomy uri structure. To visually illustrate it:

  • Root_domain/CPT name/ - This is a page that shows all "decoration products" CPT's posts.

  • Root_domain/CPT name/category - This is an archive page that shows top level category's posts.

  • Root_domain/CPT name/category/subcategory - This is an archive page that shows subcategory's posts.

  • Root_domain/CPT name/category/subcategory/post-title - This is a post within a certain top category and subcategory.

I based on this post:

  • wordpress.stackexchange/questions/39500/how-to-create-a-permalink-structure-with-custom-taxonomies-and-custom-post-types

to create two level taxonomy uri structure for "decoration products" CPT. Anyway there were some fixes to do, for instance, double slash in the uri. I fixed it with this helpful post:

  • wordpress.stackexchange/questions/75157/how-to-fix-a-double-slash-in-custom-permalinks-with-hierarchical-taxonomys

I create all my code based on above two posts and put it in functions.php. You can see it in pastebin.

I thought, that everything works well until I’ve realized that there’re some problems with the manner that WordPress handle CPT top-categories and child-categories URIs. These are the problems:

PROBLEM 1:

This interior illumination's post:

Root_domain/productos-decoracion/iluminacion/iluminacion-interior/crea-ambientes-interior-acogedores

When I click on breadcrumb link “Iluminación interior” of this post, it must show “Iluminación interior” archive page, in the following uri:

Root_domain/productos-decoracion/iluminacion/iluminacion-interior

Instead of 404 Page not Found (Error 404: Página no encontrada). If I change manually browser's uri to:

Root_domain/productos-decoracion/iluminacion-interior

404 Page not Found error disappear and everything works well.

If I remove 'hierarchical'=>true from 'rewrite' in function my_taxonomies_product() (you can find it above pastebin function.php code) and I click on breadcrumb link “Iluminación interior”, it shows archive page like I want but in the wrong uri:

Root_domain/productos-decoracion/iluminacion-interior

"Interior illumination" (iluminación interior) is a subcategory of "Illumination" and the uri doesn’t show it, it should be: Root_domain/productos-decoracion/iluminacion/iluminacion-interior

This problems happens with all the other posts that have a top category and a subcategory.

PROBLEM 2:

The second problems comes when I click on breadcrumb link “Productos decoración” of many posts or categories pages, for instance here (Root_domain/productos-decoracion/iluminacion/iluminacion-interior/crea-ambientes-interior-acogedores ). Instead of bring to Root_domain/productos-decoracion (that’s what i want) it adds %field% to the end of the uri. %field% comes from functions.php code included above.

I've tried everything but with no success. I would really appreciate any help to fix this problems.

Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Dec 13, 2015 at 14:40 MazgortaMazgorta 216 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

After many headaches, I've spent days and hours finding a solution. I've find a solution for two problems commented above. I hope it helps to someone:

PROBLEM 1: I realized that when I changed Permalink to Plain (wordpress admin > settings > permalink > Plain) and when I click in Breadcrumb link "iluminacion interior" it works OK (no more 404 error page). I tried the same in others subcategory's archive page and It works OK also. But I want "pretty permarlinks" I don't want this kind of url: ../?producto=crea-ambientes-interior-acogedores, so I’ve rewritten "/%category%/%postname%.html" in permarlinks > Custom Structure.

So the problem was that servers doesn't understand "pretty permalinks: /%category%/%postname%.html", then servers returns 404 error in subcategory's archives page when WordPress request this kind of url: rootdomain.es/productos-decoracion/iluminacion/iluminacion-interior To solve the problem I've added $newRules in function mmp_rewrite_rules in function.php, so the servers can understand WordPress requests. This is the new function mmp_rewrite_rules http://pastebin/AMELcM6r I've done the same for the other CPT Proyectos.

PROBLEM 2: This problem was easier to solve. I replace ‘has_archive’ => true, for ‘has_archive’ => ‘productos-decoracion’, in my_custom_post_product() function. ’productos-decoracion’ is the slug for the parent archive page. I do the same with proyectos CPT.

Now parent archive page url will be: …/productos-decoracion, no more .../productos-decoracion/%field%. Now we must create two file named: “archive-producto.php” and “archive-portfolio.php” (archive-CPT NAME.php) to show the content of parent archive pages. That’s all.

Post a comment

comment list (0)

  1. No comments so far