如何使用条件注释定义文件/页面


How to define a file/page using conditional comments?

我很好奇如何利用条件注释来用PHP定义页面/文件。

这项技术用于WordPress;可以通过在文件顶部插入条件注释来定义模板文件。然后,当您选择此模板时,应用程序就会知道要使用哪个文件。像这样的东西;

<?php
/*
Template Name: Snarfer
*/
?>

我想指出的是,我不想在WordPress环境中执行这个动作,但我很好奇他们是如何做到的。

那么如何在PHP中使用这种技术呢?我假设需要创建一个函数、一个类或一系列函数和类。

我试过搜索,但可能我在搜索中使用了错误的关键字。

如果有人能给我介绍一些文档或教程,我将不胜感激。

我认为你必须在wordpress archive.php上给出条件,以检查你喜欢使用哪个自定义帖子类型,你可以在这里定义你喜欢调用哪个templete文件。

<?php if ( 'news' == get_post_type() ) { ?>
            <div class="container">
                    <?php
                        /* Include the Post-Format-specific template for the content.
                         * If you want to overload this in a child theme then include a file
                         * called content-___.php (where ___ is the Post Format name) and that will be used instead.
                         */
                        get_template_part( 'news-blog', get_post_format() ); 
                    ?>
            </div>
    <?php } else { ?>
Wordpress Default call
<?php } ?>

get_template_part( 'news-blog', get_post_format() );中有新闻博客是你模板的文件名。例如news-blog.php