首页 > 解决方案 > 将我的帖子从 single.php 导航到自定义模板

问题描述

我的网站上有一些帖子是通过 single.php 发布的,现在我有一个新页面。其中有一些我想使用自定义模板的帖子,但我的帖子是 single.php 模板。谁能帮我写一些我可以在 single.php 中编写的代码,以便我可以使用帖子 ID 进行导航。?在我的自定义模板中,我应该使用什么代码来构建它..?

标签: phpwordpress

解决方案


单.php

<?php get_header(); ?>

    <div id="primary" class="content-area">
        <main id="main" class="site-main" role="main">

            <?php
            if (have_posts()) : while (have_posts()) : the_post();

                $custom_arr = array(1,2,3,4);
                if (in_array(get_the_ID(), $custom_arr)) {
                    get_template_part( 'parts/loop', 'single-custom' );
                }
                else {
                   get_template_part( 'parts/loop', 'single' ); 
                }

            endwhile; else :

                get_template_part( 'parts/content', 'missing' );

            endif;
            ?>
        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_footer(); ?>

然后创建两个单独的文件/parts/loop-single.php/parts/loop-single-custom.php

而不是通过 id 映射,您可以通过自定义类别或帖子上的自定义字段进行检查,硬编码 ID 的数组只是不好的做法


推荐阅读