我有一个名为推荐的自定义帖子类型,其中有几个自定义字段。其中一个自定义字段是单选按钮选项,它询问"这是主页特色推荐吗?"并且有 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'
))
看到我做错了什么吗?
我使用插件"类型"来创建我的自定义字段,如果这可能会有所作为。
真的需要帮助!谢谢!
根据
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'
)
);