2013-05-04 40 views
0

編輯: 好吧,我不能用is_page條件處理它。所以我寫了下面的代碼 - 效果很好,但不是很好。有什麼建議麼?在循環內的單個頁面上獲得不同的頁面模板

<?php $args = array('post_type' => 'page');?> <!-- get only pages --> 

<?php query_posts($args); ?> 

<?php while (have_posts()) : the_post(); ?> <!-- loop with pages starts --> 

<?php $id = get_the_ID(); ?> <!-- get post/pageID --> 

<?php if ($id == 5) : include(get_stylesheet_directory() . '/page_special.php'); 

     else: include(get_stylesheet_directory() . '/page_all.php'); 

     endif; 
?> 

我試圖使用一個頁面佈局的wordpress主題。爲此 我編輯我的page.php文件到這一點,得到了不同的標題的頁面

<?php $args = array('post_type' => 'page'); ?> 
<?php query_posts($args); ?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

<section id="<?php the_title(); ?>"> 

<header class="entry-header"> 
<h1 class="entry-title"><a name="<?php the_title(); ?>"></a>&nbsp;</h1> 
</header> 
<div class="entry-content"> <?php the_content(); ?> </div> 

</section> 

<?php endwhile; endif; ?> 

的& 內容現在我想插入如果條件

is_page ('about') -> get_page_template ('about')

或類似的東西但WordPress的Codex說,在一個循環內 "is_page"標籤不起作用。

我不想將我所有的不同部分模板寫入一個,並且在同一個頁面模板中寫入 。當您在頁面上時,有沒有辦法調用不同的 頁面模板?

+0

如果裏面...那麼......其他...... – 2013-05-04 19:31:24

+0

好吧,thx丹尼斯。這正是我所期待的。 – mobsteady 2013-05-04 20:08:17

回答

0
if (is_page('about')) { 

    $page_template = dirname(__FILE__) . '/about_template.php'; 
} 

這應該有效。

+0

感謝sisyphus,但在我的情況下,這是行不通的。 Wordpress Codex說: 「由於某些全局變量在循環過程中被覆蓋,is_page()將不起作用。要在循環後使用它,必須在The Loop之後調用wp_reset_query()。 – mobsteady 2013-05-04 20:11:08

+0

是的。如果您使用query($ args)來獲取帖子,那麼您應該在循環結束時使用wp_reset_query()。 – 2013-05-05 20:29:09

0

使用template_redirect功能,在你的主題functions.php文件,如:

function so16377966_template_redirect() 
{ 
    if(is_page('about')) 
    { 
     include(get_template_directory() . '/about_template.php'); 
     exit(); 
    } 
} 
add_action('template_redirect', 'so16377966_template_redirect'); 
+0

謝謝diggy。但我認爲你誤會了我。 整個網站是一個單頁網站。 菜單鏈接到#page2#page3等 因此,使用template_redirect函數及其is_page('about')條件解決方法不起作用,因爲我們將永遠不會直接進入頁面'about'。你懂我的意思嗎? – mobsteady 2013-05-04 20:38:55

0

你可以試試這個循環

<?php 
// get the permalink that is http://www.somesite.com/about/ 
$page = get_permalink(); 
    // extract will give you this Array ([dirname] => http://www.somesite.com/about [basename] => about [filename] => about) 
extract(pathinfo($page)); 
// now you can use $filename to test if you are in the right page 
if ($filename == 'about') { 
$page_template = dirname(__FILE__) . '/about_template.php'; 
} 
    ?> 

提取物是如何工作的 http://php.net/manual/en/function.extract.php