在Wordpress中,我试图在循环中显示数据。在这个特定的循环中,我想要包含具有以下元数据的所有项目:
red
blue
green
我有以下数组:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'people',
'meta_key' => 'role',
'meta_compare' => '=',
'orderby' => 'title',
'order' => 'ASC',
'meta_value' => 'red',
)
);
这会显示red
的项目。但是如何添加到数组中以显示所有项目?
功能代码在这里:
if($posts)
{ ?>
<h3 class="col-md-12">Red & Blue & Green</h3>
<?php
foreach($posts as $post)
{
?>
<div class="thumb-frame col-md-3 col-xs-6 pull-right">
<a href="#detail<?php echo get_the_id(); ?>" onClick="showDetail('<?php echo get_the_id(); ?>');">
<figure class="thumb">
<img class="img-responsive" src="<?php the_field('portrait_image');?>" alt="<?php the_title(); ?>"/>
<figcaption>
<span><?php the_title(); ?></span>
</figcaption>
</figure>
</a>
</div>
<?php
$num++;
}
}
?>
我是PHP的新手并且不确定语法,但是应该有OR运算符,还是嵌套数组?
答案 0 :(得分:0)
我不是WordPress开发人员,但我猜你可以通过从数据库中检索3组帖子来获取这些帖子:
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'people',
'meta_key' => 'role',
'meta_compare' => '=',
'orderby' => 'title',
'order' => 'ASC',
'meta_value' => 'red',
));
$posts += get_posts(array(
'numberposts' => -1,
'post_type' => 'people',
'meta_key' => 'role',
'meta_compare' => '=',
'orderby' => 'title',
'order' => 'ASC',
'meta_value' => 'blue',
));
$posts += get_posts(array(
'numberposts' => -1,
'post_type' => 'people',
'meta_key' => 'role',
'meta_compare' => '=',
'orderby' => 'title',
'order' => 'ASC',
'meta_value' => 'green',
));
现在$posts
数组将包含3个结果的联盟。
答案 1 :(得分:0)
尝试
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'people',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'key'=>'role',
'value'=>array('red', 'blue', 'green'),
'compare'=>'OR'
)
)
);
查看WP_Query
答案 2 :(得分:0)
使用更改查询尝试,它可能会帮助您
query_posts( array(
'post_type' => 'people',
'meta_query' => array(
array(
'key' => 'role',
'value' => 'red',
'compare' => 'LIKE'
),
array(
'key' => 'role',
'value' => 'blue',
'compare' => 'LIKE'
)
)
) );
<?php
if ( have_posts() ) {
echo "<h3 class="col-md-12">Red & Blue & Green</h3>";
while ( have_posts() ) {
the_post();
**// Your loop code**
}
else {
echo wpautop( 'Sorry, no posts were found' );
}
?>