drj26159 2018-06-13 11:08
浏览 30
已采纳

获得post meta显示没有结果

i have a problem with get_post_meta. Normally it works, but with this query for same reason it doesn't.

So I have 12 custom post called official matters, which are creating tabs with accordion on home page. With the post meta I want to create an if statement to check if the tab will be a link or an outgoing link or a tab.

The problem is that the get_post_meta is passing only info from one post. So after var_dump I got only array(1) { ["link"]=> string(0) ""}

Could you please tell me where is the reason?

<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条回答 默认 最新

  • dsf323233323332 2018-06-13 13:47
    关注

    On this line

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

    The $meta variable is overwrittenon each iteration of the loop, so when used later in the HTML the $meta array only has the data for one post.

    Replace with this

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

    And access in the foreach loop using

    $value['meta'][$key];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c