【问题标题】:Displaying multiple items from same array, that have different metadata显示来自同一数组的多个具有不同元数据的项目
【发布时间】:2014-03-12 10:48:27
【问题描述】:

在 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 运算符,还是嵌套数组?

【问题讨论】:

标签: php arrays wordpress


【解决方案1】:

试试

$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】:

尝试使用更改查询,它可能会对您有所帮助

query_posts( array(
    'post_type' => 'people',
    'meta_query' => array(
        array(
            'key' => 'role',
            'value' => 'red',
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'role',
            'value' => 'blue',
            'compare' => 'LIKE'
        )
    )
) );

Reference from scribu

<?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' );
}
?>

【讨论】:

  • 这是有道理的,但我无法让它返回任何东西。
  • @PeteNorris 答案已编辑。尝试替换循环。
  • 感谢您的尝试 - “抱歉,未找到任何帖子”
  • 哦,所以请先插入帖子
  • 我不明白...有帖子。如果我在顶部的示例中运行代码,它会返回一个帖子。
【解决方案3】:

我不是 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 个结果的联合。

【讨论】:

  • 感谢您的尝试,但 $post 变量只会被数组的每次迭代覆盖
  • 嘿@PeteNorris,不是不会,您需要使用 $posts += 而不是 $posts = 。这是一个基本的 PHP 数组运算符,试试吧。
  • 它只返回第一个数组的值
  • 这很奇怪,尝试使用 3 个数组来获取 3 个数据集,然后使用 array_merge 将它们连接起来
  • 或者它只是返回红色值,因为这就是数据库中存在的全部内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多