Wordpress查询页面与某些模板

时间:2012-08-23 07:47:23

标签: 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

的页面模板文件

1 个答案:

答案 0 :(得分:21)

如果省略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)
?>