首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在wordpress中定制post类型

如何在wordpress中定制post类型
EN

Stack Overflow用户
提问于 2022-04-20 03:33:47
回答 1查看 35关注 0票数 0

我想为每个类别定制不同的帖子。但是我做不到,请你看看我,我的错误在哪里?

例如,我有两个类别。

1 window 2 android

我希望windows类别调用内容-windows-细节s.php文件。

android内容-app-细节s.php

但现在,Windows类别将调用app-细节s.php文件。我需要关闭调用windows-diets.php

代码single.php

代码语言:javascript
复制
    <?php

    /**

     * The template for displaying all single posts

     *

     * @link https://developer.wordpress.org/themes/basics/template-hierarchy/#single-post

     *

     * 

     */



    get_header();

    $p_id = get_the_ID();       
    $terms = get_the_terms( $p_id, 'category' ); 
    $is_tip = false;
    $cat_id = 0;
    foreach($terms as $term) { 
        if( !is_app_cat($term->term_id)){
            $is_tip = true;
            $cat_id = $term->term_id;
            break;
        }
        else{
            if($term->parent != 0){
                $cat_id = $term->term_id;
            }
        }
    } 
    if($is_tip){
        $is_topten = false;
        foreach($terms as $term) { 
            if($term->slug == 'windows'){
                $is_topten = true;
                break;
            }
        }
        if($is_topten){
            get_template_part( 'template-parts/content', 'windows-details' ); 
        }
        else{
            get_template_part( 'template-parts/content', 'single' );
            
        }
    }
    else{
        get_template_part( 'template-parts/content', 'app-details' );
    }



    ?>


    <?php

    get_footer();

EN

回答 1

Stack Overflow用户

发布于 2022-04-20 05:45:06

WordPress已经内置了对这类东西的支持(我相信您正在使用WordPress)。你只需要重命名你的文件。

而不是使用category-windows.php,而不是内容-windoes细节s.php

安卓系统也是如此,它是category-android.php

这样,在显示每个类别的帖子时,将使用正确的文件。

关于这方面的法典页是:https://developer.wordpress.org/themes/basics/template-hierarchy/#examples

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71933788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档