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

时间:2013-02-23 03:38:41

标签: php wordpress function

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

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

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

我想指出,我不希望在WordPress环境中执行此肌动蛋白,但我很好奇他们是如何做到的。

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

我尝试过搜索,但也许我在搜索中使用了错误的关键字。

如果有人能指出我的文档或教程,我将非常感激。

1 个答案:

答案 0 :(得分:-1)

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

<?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