---已更新---
好了,现在我对您正在做的事情有了更多的了解(查询CPT并使用模板文件将其显示在页面上),下面是我如何处理类似功能的:
在我的一个网站上,我有一个页面描述了在特定区域的徒步旅行,下面我查询了CPT“徒步旅行”的所有自定义帖子,这些帖子有一个自定义字段,其中保存了徒步旅行的(未来)日期(这是一个有计划日期的引导徒步旅行,还有一个供人们注册徒步旅行的表单)。
徒步旅行页面的模板文件开始于前面提到的
if (have_posts()) : while (have_posts()) : the_post();
然后是一些HTML,其中
the_content();
以便在页面上显示关于在该区域徒步旅行的通用措辞。
低于我使用的get_posts(); 获取即将进行的徒步旅行的所有CPT帖子,以便不显示以前的徒步旅行
    global $post;
    $args = array (
    \'post_type\' => \'hike\',
    \'meta_key\' => \'hike_date_start\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => -1);
    $myposts = get_posts($args); 
    foreach ( $myposts as $post ) : setup_postdata( $post );
    $exp_date = strtotime(get_post_meta($post->ID,\'hike_date_start\',true));
    $today = time();
    if ($today <= $exp_date) {
    $hikedate = get_post_meta($post->ID,\'hike_date_start\',true);
    $hikedate2 = get_post_meta($post->ID,\'hike_date_end\',true);
    $hikedateend = strtotime(get_post_meta($post->ID,\'hike_date_end\',true));
    $hikerating = get_post_meta($post->ID,\'hike_rating\',true);
    $hikeleader = get_post_meta($post->ID,\'hike_leader\',true);
    $hikecontactph = get_post_meta($post->ID,\'hike_contact_phone\',true);
    $hikecontactem = get_post_meta($post->ID,\'hike_contact_email\',true);
    $hikedirections = get_post_meta($post->ID,\'hike_directions\',true);
 下面是一些HTML,以我想要的格式显示数据,如下所示:
    <div class="hikeentry">
      <div class="time">
      <div class="month"><?php echo date(\'M\', $exp_date); ?></div>
      <div class="wkday"><?php echo date(\'l\', $exp_date); ?></div>
      <div class="day"><?php echo date(\'d\', $exp_date); ?></div>
      <div class="year"><?php echo date(\'Y\', $exp_date); ?></div>
   </div>
   <div class="hikedata">
     <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
     <div class="hikemeta">
     <div class="left">
     <span>Rating:</span> <?php echo $hikerating; ?><br />
     <span>Leader:</span> <?php echo $hikeleader; ?><br />
     <span>Contact:</span> <a href="mailto:<?php echo antispambot($hikecontactem); ?>"><?php echo antispambot($hikecontactem); ?></a>    <?php echo $hikecontactph; ?> 
     </div>
    <div class="left bordered rounded bkgd"><a href="<?php the_permalink() ?>">Click here to read full<br />description and Sign Up</a>
    </div>
    </div>
    <?php the_excerpt(); ?>
    </div>
    </div><!-- end .hikeentry -->
 此外,在完成foreach循环时,重置postdata也很重要,如下所示
    } endforeach; wp_reset_postdata();
 因为我使用
get_posts($args); 而不是
wp_query(); 然后不需要重置查询,只需重置postdata即可。
我没有特别使用the_ID(); 但是您可以,在foreach循环中的任何需要它的地方,只需调用the_ID(); 因为它是使用setup_postdata(); 
我希望这能有所帮助,在评论中提出更多问题,我很高兴解释更多关于我如何/为什么使用这个。。。。。。我更喜欢使用WP内置方法来检索和使用数据,而不是ACF,但我喜欢ACF使我的用户可以轻松地输入自定义数据。:-)
---前面的答案,上面更新了--如果不了解您的具体用途,就很难确切知道如何给您提供建议,但如果我正确理解了您的问题,您是在为特定页面创建模板文件,是吗?
在这种情况下,在已添加页面并指定要使用的新模板的“页面>添加新”中,模板文件的顶部仍然需要:
if (have_posts()) : while (have_posts()) : the_post();
然后在需要使用ID的地方
the_ID();我使用它将页面的ID(在当前版本的WP中,无论出于何种目的,ID都与“post”相同,现在几乎所有内容都是“post”)分配给页面上的特定元素。
如果你能发布更多关于你正在努力实现的细节,这将有助于我们给你更好的建议。