2013-02-23 39 views
0

我很好奇如何利用條件註釋來定義PHP的頁面/文件。如何使用條件註釋定義文件/頁面?

該技術在WordPress中使用;可以通過在文件頂部插入條件註釋來定義模板文件。然後當你選擇這個模板時,應用程序知道使用哪個文件。像這樣的東西;

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

我想指出的是,我不希望在WordPress的環境中執行此肌動蛋白,但我很好奇,他們是如何做到這一點。

那麼如何在PHP中使用這種技術?我假設需要創建一個函數或一個類或一系列函數和類。

我嘗試過搜索,但也許我在搜索中使用了錯誤的關鍵字。

如果有人能指向我的文檔或教程,我會非常感激。

+0

WP讀取所有php腳本一次,提取這些評論元信息並保留一個單獨的列表。 – mario 2013-02-23 03:41:14

+0

@mario謝謝你,我剛剛根據你的評論進一步細化了我的搜索條件,並提出了兩種可能性來做到這一點; 1)帶有getDocComment的ReflectionClass或2)token_get_all。感謝推動,我將研究這兩個,看看它是如何。歡呼 – robolist 2013-02-23 04:01:53

+0

WP使用一種非常簡單的技術,我相信,也許是一個正則表達式。如果您希望親自看到它,這些都在WordPress源代碼中。 – kjetilh 2013-02-23 13:12:29

回答

-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