在Wordpress中为每个类别的帖子创建不同的页面类型


Create different page type for posts of each category in Wordpress

我使用的是wordpress模板。我有很多类别,比如,冒险,体育,新闻。现在,每个类别中都有帖子,我知道singlep.php正在生成所有类型的帖子。假设,如果你点击新闻,它会显示所有与新闻相关的帖子。每个帖子都被标识为相同的页面类型"帖子"。但是,当点击Adventure时,我想调用一个单独的Post.php文件,而不是single.php。意思是,当我浏览Adventure类别的帖子时,它应该来自example.php。Example.php是我想要链接到Adventure的文件。我需要帮助。就像

if (Category == Adventure)
call the example.php
else
single.php

我应该把重点放在哪里。有什么与functions.php有关的吗?此外,wordpress如何调用Single.php?这样我就可以指示它也调用我的example.php

你试过编辑你的single.php吗?

in_category ( int|string|array $category, int|object $post = null )

来自wordpress网站本身的样本

if ( in_category('fruit') ) {
    include 'single-fruit.php';
} elseif ( in_category('vegetables') ) {
    include 'single-vegetables.php';
} else {
    // Continue with normal Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    // ...
}

in_category。这也可以是一个参考,模板层次结构:单个帖子。


这可以是一个选项,通过添加自定义标题文件和编辑single.php文件,为单个帖子的每个类别自定义标题

single.php文件的最顶部

if(in_category('apple')){
    get_header('fruits');
}elseif(in_category('cabbage')){
    get_header('vegetables');
}else{
    get_header();
}

然后创建您自己的自定义标头,其确切名称如上面的示例代码header-fruits.php&header-vegetables.php