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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分