获得post meta显示没有结果

时间:2018-06-13 11:08:34

标签: php wordpress

我遇到了get_post_meta的问题。通常它可以工作,但由于同样的原因,这个查询没有。

所以我有12个自定义帖子称为官方事务,它们正在主页上创建带有手风琴的标签。使用post meta我想创建一个if语句来检查选项卡是链接还是外向链接或选项卡。

问题是get_post_meta只从一个帖子传递信息。所以在var_dump之后我只得到了array(1) { ["link"]=> string(0) ""}

你能告诉我原因在哪里吗?

<div id="collapse-official-matters" class="col-xl-12">
<div class="official-matters-group">

 <?php $args = array('post_type' => 'official-matters', 'showposts' => 20, 'orderby' => 'date', 'order' => 'ASC',); $the_query = new WP_Query( $args ); ?>
 <?php $i = 0; $row = 0;
  while ( $the_query->have_posts()) {

    $the_query->the_post();
    if ($i === 4) {
      $i = 0;
      $row++;
    }

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


    $postsData[$row]['tabs'][$i] = [
      'id' => get_the_id(),
      'thumbnail' => get_the_post_thumbnail_url(),
      'title' => get_the_title()
    ];

    $postsData[$row]['content'][$i] = [
      'id' => get_the_id(),
      'excerpt' => get_the_excerpt()
    ];

    $i++;
  }
 ?>

 <div id="accordion">
 <?php foreach($postsData as $key => $value) :
   $tabs = $value['tabs'];
   $contents = $value['content'];
   ?>
     <div class="customRow">
       <?php foreach($tabs as $key => $value) : ?>
         <div class="info-tab col-xl-3">
           <div class="info-img">
             <img src="<?php echo $value['thumbnail'] ?>" />
           </div><!-- .news_img -->
           <?php if ($meta['isLink'] === '1') {
              if ($meta['outgoingLink'] === '0') {  ?>
                <a href="<?php echo $meta['link'] ?>" class="info-title collapsed" role="button">
                  <?php echo $value['title'] ?>
                </a><!-- .info-title -->
              <?php } else { ?>
                <a href="<?php echo $meta['link'] ?>" class="info-title collapsed" role="button" target="_blank">
                  <?php echo $value['title'] ?>
                </a><!-- .info-title -->
              <?php } ?>
          <?php } else { ?>
            <a data-toggle="collapse" data-target="#collapse-<?php echo $value['id'] ?>" href="#collapse-<?php echo $value['id'] ?>" class="info-title collapsed" role="button" aria-expanded="false" aria-controls="collapse-<?php echo $value['id'] ?>">
              <?php echo $value['title'] ?>
            </a><!-- .info-title -->
           <?php } ?>

         </div><!-- .info-tab -->
       <?php endforeach; ?>

       <?php foreach($contents as $key => $value) : ?>
         <div class="official-matters-content">
           <div class="info-tab-content collapse" id="collapse-<?php echo $value['id']; ?>" data-parent="#accordion">
             <div class="card card-body">
               <?php echo $value['excerpt'] ?>
             </div><!-- .card -->
           </div><!-- .info-tab-content -->
         </div><!-- .official-matters-content -->
       <?php endforeach; ?>
     </div>
   <?php endforeach ;?>
 </div>
 <?php wp_reset_postdata(); ?>
 <?php if (false) : ?>
 <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
       <div class="info-tab col-xl-3">
         <div class="info-img">
           <?php the_post_thumbnail(); ?>
         </div><!-- .news_img -->
         <a data-toggle="collapse" href="#collapse-<?php the_ID(); ?>" class="info-title" role="button" aria-expanded="false" aria-controls="collapse-<?php the_ID(); ?>">
           <?php the_title(); ?>
         </a><!-- .info-title -->
       </div><!-- .info-tab -->

       <div class="official-matters-content">
         <div class="info-tab-content collapse" id="collapse-<?php the_ID(); ?>">
           <div class="card card-body">
               <?php the_excerpt(); ?>
           </div><!-- .card -->
         </div><!-- .info-tab-content -->
       </div><!-- .official-matters-content -->


 <?php
   endwhile;
   wp_reset_postdata();
 ?>
   

1 个答案:

答案 0 :(得分:1)

在这一行

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

$ meta变量在循环的每次迭代中被覆盖,因此当稍后在HTML中使用时,$ meta数组仅包含一个帖子的数据。

替换为

$postsData[$row]['meta'][$i] = get_post_meta( $post->ID, 'official_matters_details', true );

使用

访问foreach循环
$value['meta'][$key];