Wordpress multiple template for same category with different custom post type

不问归期 提交于 2021-02-11 15:34:24

问题


I have this structure for my Wordpress site :

  • Custom post type 1
    • Category a
      • Term a-1
      • Term a-2
      • ...
  • Custom post type 2
    • Category a (same category than CPT 1)
      • Term a-1
      • Term a-2
      • ...

I would like to have different template for "Category a" archive page and single term page depending of current post type. How can I achieve this?

In other words, I want these URLs each have their own template :

  1. http://www.example.com/custom-post-type-1/category-a/
  2. http://www.example.com/custom-post-type-1/category-a/generic-single-term
  3. http://www.example.com/custom-post-type-2/category-a/ (same as 1 but different CPT URL)
  4. http://www.example.com/custom-post-type-2/category-a/generic-single-term (same as 2 but different CPT URL)

Thank you!


回答1:


You need to use the taxonomy.php template file (and its derivatives) or the category.php template file (and its derivatives) , ( which is is only for use with the Category taxonomy ) .

If you want an archive index for some custom taxonomy, you would use taxonomy-{taxonomy}.php template file. So, if you have created a custom taxonomy, my-taxonomy, the archive index for this taxonomy would use the taxonomy-my-taxonomy.php template file.

If you want an archive index for some terms inside your custom taxonomy, you use taxonomy-{taxonomy}-{term}.php template file. So, if you've got a term, my-term, for your custom taxonomy, the archive index for this term would be taxonomy-my-taxonomy-my-term.php.

(Note:a single word for name of taxonomy and or terms is better I Think.)

More info on the template hierarchy CODEX page and taxonomy specific Here



来源:https://stackoverflow.com/questions/32875274/wordpress-multiple-template-for-same-category-with-different-custom-post-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!