2013-07-07 48 views
0

我創建了一個查詢來調用當前父頁面的所有子頁面。這很好,但是每個子頁面都有一個自定義模板。我不知道如何添加一個自定義查詢參數來說明模板。目前我查詢每個子頁面的the_content,但是不包含該模板。WordPress的:查詢具有每個頁面的特定模板的子頁面

任何人都可以幫助我修改查詢嗎?

<?php $children = get_pages( 
    array(
     'sort_column' => 'menu_order', 
     'sort_order' => 'ASC', 
     'hierarchical' => 0, 
     'parent' => $post->ID, 
     'post_type' => 'projects', 
     'meta_query' => array(
      array(
       'key' => '_wp_page_template', 
       'value' => 'template-city.php', // template name as stored in the dB 
      ) 
     ) 
    )); 

foreach($children as $post) { 
     setup_postdata($post); ?> 
    <div class="section-container"> 
     <?php the_content(); ?> 
    </div> 
<?php } ?> 

回答

1

您可以使用get_post_meta!

隨着template_redirect行動:

function my_page_template_redirect() 
{ 
    global $post; 
    if(get_post_type($post) == 'projects') 
    { 
     $tpl = get_post_meta($post->ID, '_wp_page_template'); 
     if ($tpl) { 
      include(get_template_directory() . $tpl); 
      exit(); 
     } 
    } 
}  
add_action('template_redirect', 'my_page_template_redirect'); 
+0

感謝您的回覆!那麼適合我的模板? – TechyDude

+0

我想通了,謝謝你!我修改了你的代碼:<?php $ tpl = get_post_meta($ post-> ID,'custom_post_template',true); \t \t \t include($ tpl); ?> 感謝您的幫助! – TechyDude

+0

我真的很晚,但現在Codex建議使用'template_redirect'鉤子來實現這個http://codex.wordpress.org/Plugin_API/Filter_Reference/template_include –

1

我覺得get_pages功能不使用meta_query,你需要做的是這樣的:

$children = get_pages( 
    array(
     'sort_column' => 'menu_order', 
     'sort_order' => 'ASC', 
     'hierarchical' => 0, 
     'parent' => $post->ID, 
     'post_type' => 'projects', 
     'meta_key' => '_wp_page_template', 
     'meta_value' => 'template-city.php', 
    )); 

或使用使用meta_queryget_posts功能。

+0

有沒有辦法讓每個孩子交了meta_value? – TechyDude

相關問題