0

我有一个名为 testimonials 的自定义帖子类型,其中有一些自定义字段。其中一个自定义字段是单选按钮选项,它询问“这是主页特色推荐吗?” 并且有 2 个选项,是或否,两者的值都是 1 和 2。

我试图仅显示该单选按钮选项的值设置为“1”(设置为是)的帖子,但它似乎不起作用。

我在页面上显示了所有帖子自定义字段信息,但它也显示了以“2”为值的帖子。我还让它在帖子中显示单选按钮的值,并将它们设置为“1”或“2”。我只是在查询中遇到问题,只显示值为“1”的查询。

这是我的代码:

<?php 

                // args
                $args = array(
                    'post_type' => 'testimonials',
                    'posts_per_page' => 4,
                    'order' => 'ASC',
                    'meta_query' => array( 
                                        'key' => 'homepage-testimonial',
                                        'value' => '1'
                                      )

                );

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

                // The Loop
                if( $testimonial_query->have_posts() ): $count = 0; 
                    while ( $testimonial_query->have_posts() ) : $testimonial_query->the_post(); $count <= 2; $count++;

                        $testimonial_homepage_option = types_render_field("homepage-testimonial", array("raw"=>"true"));
                        $testimonial_img = types_render_field("testimonial-image", array("output"=>"html"));
                        $testimonial_name = types_render_field("testimonial-name", array("raw"=>"true"));
                        $testimonial_para = types_render_field("testimonial-para", array("raw"=>"true"));
                ?>

                                <div class="grey-cta-item">

                                            <?php echo $testimonial_homepage_option; ?>
                                            <?php echo $testimonial_img; ?>
                                            <?php echo $testimonial_name; ?>
                                            <p class="yellow-title-caps"> <?php the_title() ?> </p>
                                            <?php echo $testimonial_para; ?>
                                </div>

                    <?php endwhile; ?>
                <?php endif; ?>

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

我也尝试了这些方法来让它正确显示:

'homepage-testimonial' => 1

'meta_query' => array( 
                                    array(
                                        'key' => 'homepage-testimonial',
                                        'value' => '1'
                                      ))

看到我做错了什么吗?

如果这可能会有所不同,我使用插件“类型”来创建我的自定义字段。

真的需要帮助!谢谢!

4

1 回答 1

0

根据WP_Query 类参考,您还希望在顶级$args数组中有一个 meta_key 索引。所以:

$args = array(
    'meta_key' => 'homepage-testimonial', # add this in
    'post_type' => 'testimonials',
    'posts_per_page' => 4,
    'order' => 'ASC',
    'meta_query' => array( 
        'key' => 'homepage-testimonial',
        'value' => '1'
     )
);
于 2013-10-10T20:08:51.830 回答