【问题标题】:Create different page type for posts of each category in Wordpress为 Wordpress 中每个类别的帖子创建不同的页面类型
【发布时间】:2016-07-18 08:49:30
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您是否尝试编辑您的 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 post


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

    single.php 文件的最顶部

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

    然后创建您自己的自定义标头,其名称与上面的示例代码一样为header-fruits.phpheader-vegetables.php

    【讨论】:

    • 我不想编辑 single.php。而是想创建一个类似的文件example.php。因为,我想在特定类别的每个帖子的标题中添加一些代码,其他类别的帖子将与之前从 single.php 调用的相同。
    • 我需要一点帮助,这在哪个文件中:- if ( have_posts() ) : while ( have_posts() ) : the_post();
    • @Codelife 更新了答案,你在找什么?
    • @Codelife 是The Loop 的起点,它可能出现在每个页面上(当然,这需要循环)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    相关资源
    最近更新 更多