单个自定义 post 类型的多个模板

Multiple templates for single custom post type

我正在为一家公司开发网站,我需要能够为一种自定义 post 类型创建多个模板文件。我有三个着陆页,它们都以不同的方式显示相同的 services/custom post 类型 post 列表;但是,根据用户所在的页面,我希望更改内部模板。

例如,如果用户在图库登录页面上单击自定义 post 类型,我希望加载单个图库模板。如果用户在推荐登录页面上,我希望加载单一推荐模板等等...

我搜索了 WordPress,找到了 template_part 和端点之类的东西,但作为 WordPress 的新用户,我需要更多帮助!

如有任何帮助,我们将不胜感激。谢谢!

  1. 找出我们在哪个着陆页(图库、推荐、其他……)
  2. 使用get_template_part()
  3. 查询您的帖子

例如:

if($thispage == 'gallery') {
 get_template_part('single', 'gallery'); // the file for this one is single-gallery.php
}elseif($thispage == 'other'){
 get_template_part('single', 'other'); // the file for this one is single-other.php
}

或者那样

get_template_part('single', $thispage ); 

https://codex.wordpress.org/Function_Reference/get_template_part

为了快速解决问题,我建议这样:

链接时可以使用:

<a href="<php the_permalink();?>?template=gallery"> Gallery Page </a>

然后上单-customposttype.php

if($_POST['template'] == 'gallery') {
 get_template_part('single', 'gallery'); // the file for this one is single-gallery.php
}elseif($_POST['template'] == 'other'){
 get_template_part('single', 'other'); // the file for this one is single-other.php
}

不是最漂亮的,但推出起来又快又容易。