10

有没有办法查询具有特定模板的 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

4

1 回答 1

24

如果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)
?>
于 2012-08-23T11:40:12.797 回答