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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化