dongpo2002 2014-12-13 23:01
浏览 50
已采纳

在WordPress中的循环内循环

I'm having a problem with some PHP code in WordPress and I think the solution has to do with loops. I've had a look at http://codex.wordpress.org/The_Loop and http://codex.wordpress.org/The_Loop_in_Action but it's way over my head and I've already wasted my entire Saturday on this.

So the thing is I have a widget on my website's homepage. It displays the titles to the next 5 events happening and links to them. What I wanted to do was to also display the date when the events (they are like blog posts) were last modified.

The problem is that my code keeps returning the wrong date. I modified my post today, the 13th of December and it keeps outputting the 11th of December. I don't know where it's getting that date from. I've tried using both the_modified_date() and get_the_modified_date() with the same results. I'm guessing the function doesn't know which post to pull the date from and it has something to do with loops.

So I've been messing with the widget's code. Here's a simplified version of what I have:

foreach ($events as $event) {

  if ( $event['event_date'] >= $site_date ) {

    $lastMod = get_the_modified_date($phpformatstring);
    echo $lastMod;

    echo '<br><a href="' . get_permalink($event['ID']) . '"  >' . get_the_title($event['ID']) . '</a>';

    if ( !empty( $widget_display_count ) ) {
      $counter++;
      if ( $counter == $widget_display_count )
        break;
    }
  }
}

I really hope someone can help me. If you want to try it with the widget I'm using, it's one called Events Listing Widget

  • 写回答

2条回答 默认 最新

  • douyi3767 2014-12-13 23:35
    关注

    get_the_modified_date() needs the $post global. The plugin is iterating over the results of a wpdb query so technically there's no WP loop.

    Something like global $post; $post = get_post( $event['ID'] ); setup_postdata( $post ); right before the get_the_modified_date() call should work. Don't forget to reset the post data afterwards with wp_reset_postdata();

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)