0

在检查帖子的元键值是否为 === 1 后,我想从自定义帖子中显示帖子。但我收到错误“法律字符串偏移量'isAirConditioning'”。我做错什么了?

我创建了一个名为“Works”的自定义帖子。在这个自定义帖子中,我制作了一个元框,可以让我标记哪些工作将分配给空调、制冷或回收。然后在页面模板上,我运行一个 if 语句来检查我当前在哪个页面上显示正确的作品。如果我在空调工作页面上,则传递一个查询以从该自定义帖子中获取所有带有 meta 的帖子,然后执行第二个 if 语句来检查 $meta['isAirConditioning'] === '1 '。

 <?php
    $classes = get_body_class();
    if(in_array('page-id-233', $classes)) {
      echo '<p>do something</p>';
    }
    elseif(in_array('page-id-239', $classes)) {
      echo '<ul>';
       $args = array('post_type' => 'works', 'orderby' => 'date', 'order' => 'ASC', 'showposts' => 100);
       $the_query = new WP_Query($args);

       while ($the_query->have_posts() ) : $the_query->the_post();

       $meta = get_post_meta( $post->ID, 'portfolio_details', true );

       if ($meta['isAirConditioning'] === '1') {
  ?>
         <li class="works-wrapper col-3 col-sm-3 col-md-3 col-lg-3">
            <a class="works-img"href="<?php the_permalink() ?>" >
              <span class="works-gradient"></span>
              <?php the_post_thumbnail(); ?>
             </a><!-- .works-img -->
            <a class="works-title" href="<?php the_permalink() ?>" ><?php the_title(); ?></a>
         </li><!-- .worsk-wrapper -->

  <?php
   }
      endwhile;

      echo '</ul>';
  }
  elseif(in_array('page-id-241', $classes)) {
    echo '<p>do something</p>';
  }
 ?>

帖子正在显示,但我收到错误“非法字符串偏移 'isAirConditioning'”

4

2 回答 2

0

看官方文档。您将 $single 设置为 true 并检索一个值而不是数组。所以我猜出现错误是因为您尝试使用字符串作为数组。

于 2019-04-07T17:24:59.887 回答
0

我设法通过检查 $meta 是否为空来解决问题

if(!empty($meta)) :
   if ($meta['isAirConditioning'] === '1') {
   ?>
     <li class="works-wrapper col-3 col-sm-3 col-md-3 col-lg-3">
       <a class="works-img"href="<?php the_permalink() ?>" >
         <span class="works-gradient"></span>
         <?php the_post_thumbnail(); ?>
       </a><!-- .works-img -->
       <a class="works-title" href="<?php the_permalink() ?>" ><?php the_title(); ?></a>
     </li><!-- .worsk-wrapper -->
   <?php
  }
  endif;
于 2019-04-08T06:36:03.990 回答