2015-09-28 43 views
1

我一直在尋找答案,無濟於事。我正在構建一個頁面的WordPress模板。我有一個主頁,它使用一個頁面模板,稱爲one-page.php,它正在所有其他頁面中進行。繼承人從該模板的PHP:Wordpress單頁模板與動態投資組合部分

<?php 
      $args = array(
       'post_type' => 'page', 
       'order' => 'ASC' 
      ); 
      $the_query = new WP_Query($args);   
     ?> 

     <?php if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?> 

     <?php get_template_part('content', 'page'); ?> 

     <?php endwhile; endif; ?> 

此代碼工程很好。所有部分都可以使用content-page.php模板部分,不包括投資組合部分,我希望使用另一個帶有所有投資組合定製帖子類型的模板部分。

我試圖添加條件if語句都一page.php文件和內容page.php文件,像這樣:

<?php if (is_page('portfolio')) : ?> 
    //My portfolio custom post type loop is here 
    <? endif; ?> 

但是,這並沒有工作,要麼 - 我認爲,是因爲is_page()函數將檢查當前正在顯示的頁面,即主頁。而不是找出查詢目前正在處理的頁面 - 但我不確定。

任何人都可以幫助我瞭解如何有條件地將投資組合部分加載到單獨的模板部分?

回答

1

你能達到這個檢查網頁塞,您可以在迴路中獲得。如果是「投資組合」(或您保存的任何內容),則加載content-portfolio.php,否則content-page.php。類似這樣的:

 if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); 
    if ("portfolio" === $post->post_name) { 
     get_template_part('content', 'portfolio'); 
    } else { 
     get_template_part('content', 'page'); 
    } 
endwhile; endif; 
0

文件

<?php 
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); 
    if (is_page('portfolio')) { 
     get_template_part('content', 'portfolio'); 
    } else { 
     get_template_part('content', 'page'); 
    } 

endwhile; endif; 
?> 

設置條件創建一個名爲content-portfolio.php一個模板文件,該文件的副本content-page.php,把下面的代碼在裏面。如果顯示「呼叫」,則表示您的模板正在運行。

portfolio.php

<?php 
echo "portfolio.php"; 
die('Call'); 
?>