2012-08-23 67 views
4

有沒有辦法查詢具有特定模板的Wordpress頁面? 這是我得到的,但不顯示任何東西:Wordpress查詢頁面與某些模板

<?php $my_query = new WP_Query(array('meta_key' => '_wp_page_template', 'meta_value' => 'template-city.php')); ?> 
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> 
        <li> 
        <?php the_title(); ?> 
        </li> 
       <?php endwhile; ?> 

當然,還有頁面命名template-city.php

+0

嗯,這將如何工作?你想達到什麼目的? – SMacFadyen

+0

網站上有很多頁面 - 大陸,城市孩子,進一步有住宿兒童,或restoraunt兒童等每個頁面都有自己的頁面模板 - 所以我想顯示所有城市的查詢。希望這可以幫助? – jOpacic

+0

這聽起來像你需要wp_list_pages(),http://codex.wordpress.org/Function_Reference/wp_list_pages – SMacFadyen

回答

17

如果post_type留出WP將尋找後模板文件,你正在尋找頁面。

<?php 
    $args = array(
     'post_type' => 'page',//it is a Page right? 
     'post_status' => 'publish', 
     'meta_query' => array(
      array(
       'key' => '_wp_page_template', 
       'value' => 'template-city.php', // template name as stored in the dB 
      ) 
     ) 
    ); 
$my_query = new WP_Query($args) 
?> 
+0

是的,就是這樣!非常感謝! – jOpacic

+1

只是提醒人們看到這一點:是的,你確實需要將meta_key的值作爲數組中的數組才能工作。 –