【问题标题】:get current post title from inside another query loop从另一个查询循环中获取当前帖子标题
【发布时间】:2021-09-26 08:42:45
【问题描述】:

我有一个带有标准 while 循环的 single-page.php。在该循环中,我有另一个循环,我想输出我正在查看/查看的帖子标题,但是因为我在函数 futureEvent 的内部循环中有 get_the_title(),所以它输出的是该帖子的标题:

while (have_posts()) {
    the_post(); ?>
...
 $futureEvents = get_posts($args);
  foreach ($futureEvents as $post) {
   $futureEvents = new WP_Query($args);
    if ($futureEvents->have_posts()) {
      echo '<div class="related-content"><h3>Upcoming ' . get_the_title() . ' Events</h3></div>';
   ?>
    <article class="post-item">
     <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    </article>
    }
   }
}

在 wordpress 中,我有一个添加了三个帖子的 CPT。
我使用了 get_the_title(118),但是它硬编码了帖子的 id,但我想从我的 CPT 获取任何帖子的标题。

我尝试使用 get_the_title($post->$ID) 和 get_the_title($post_id),但仍然输出不正确的帖子标题。

谁能看到我如何从该循环中获取我正在/正在查看的帖子的标题?

【问题讨论】:

    标签: php wordpress if-statement custom-post-type posts


    【解决方案1】:

    我如何获得我正在/正在查看的帖子的标题 那个循环?

    你可以使用:

    例如,在您的 inner 循环中,您可以像这样显示上述帖子的标题:

    echo '<div class="related-content"><h3>Upcoming ' . get_the_title( get_queried_object() ) . ' Events</h3></div>';
    

    【讨论】:

      猜你喜欢
      • 2013-01-21
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 2015-06-10
      • 1970-01-01
      • 2013-10-30
      • 2019-06-23
      • 1970-01-01
      相关资源
      最近更新 更多