0

我正在尝试创建一个页面来显示我所有包含某个自定义字段的 wordpress 帖子。我认为最好的方法是创建一个“自定义页面模板”并使用新的 WP_query 来列出有问题的帖子。我创建了一个新的“页面”并使用我的新 WP_query 应用了新的“页面模板”,但是页面上没有显示任何帖子。谁能告诉我为什么这个自定义页面模板没有正确列出有问题的帖子?任何帮助深表感谢!

<?php

/**

 * Template Name: Recommended Incentives


 *

 * @package WordPress

 * @subpackage Twenty_Fourteen

 * @since Twenty Fourteen 1.0

 */



get_header(); ?>



<div id="main-content" class="main-content">







    <div id="primary" class="content-area">

        <div id="content" class="site-content" role="main">


            <?php 

// args
$args = array(

    'meta_key' => 'Incentive ID',
    'meta_value' => '20'
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

<?php wp_reset_query();  // Restore global post data stomped by the_post(). ?>


        </div><!-- #content -->

    </div><!-- #primary -->

</div><!-- #main-content -->



<?php

get_sidebar();

get_footer();
4

1 回答 1

0

谢谢安迪,当我在$args变量中指定帖子类型时,我的代码有效。我使用的代码是

'meta_key' => 'Incentive ID',
    'meta_value' => array(20,21),
    'orderby' => 'meta_value_num',
   'order' => 'ASC',
    'post_type' => 'incentives'
于 2014-06-11T15:29:46.060 回答