首页 文章

单个自定义帖子类型的多个模板

提问于
浏览
-1

我正在为一家公司开发一个网站,我需要能够为一个自定义帖子类型提供多个模板文件 . 我有三个目标网页,它们以不同的方式显示相同的服务列表/自定义帖子类型的帖子;但是,根据用户所在的页面,我希望更改内部模板 .

例如,如果用户在图库登录页面上并且他们单击自定义帖子类型,我希望加载单图库模板 . 如果用户位于推荐登录页面上,我希望加载单个推荐模板,依此类推......

我已经搜索了WordPress并找到了像template_part和endpoints这样的东西,但作为WordPress的新用户,我需要更多的帮助!

任何帮助将非常感激 . 谢谢!

2 回答

  • 0

    为了快速解决方案,我建议这样的事情:

    链接时你可以使用:

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

    然后在single-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
    }
    

    不是最漂亮的,但它很容易推出 .

  • 0
    • 找出我们的目标网页(图库,推荐书,其他,...)

    • 使用 get_template_part() 查询您的帖子

    例如:

    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

相关问题