ACF 将 CPT 字段添加到 header

ACF adding CPT fields to header

我有一个名为 'Projects' 的自定义 post 类型,我已将灵活的字段内容添加到我的 single-projects.php。我正在尝试为主 header.php 中的每个项目添加一个英雄形象,因为我希望我的英雄形象位于导航后面。我一切正常,但我的英雄形象显示在 CPT 存档页面和 CPT 单页上,但我只想在每个项目页面上显示英雄形象。

在我的 header.php 文件中我有:

<?php

  // check if the flexible content field has rows of data
  if( have_rows('project_flexible') ):

  // loop through the rows of data
  while ( have_rows('project_flexible') ) : the_row();

    if( get_row_layout() == 'project_hero_image'):
      $hero_image = get_sub_field('image'); ?>

    <div class="hero-image"><img src="<?php echo $hero_image; ?>">
    </div>

    <?php
      endif;
      endwhile;

      else :
       // no layouts found
      endif;
    ?>

也许你可以尝试只在单个页面上显示:

<?php

// check if the flexible content field has rows of data
if( have_rows('project_flexible') ):

// loop through the rows of data
while ( have_rows('project_flexible') ) : the_row();

if( get_row_layout() == 'project_hero_image'):
  $hero_image = get_sub_field('image'); ?>

// Added this if statement
<?php if(is_single()){ ?>
  <div class="hero-image"><img src="<?php echo $hero_image; ?>"></div>
<?php } ?>

<?php
  endif;
  endwhile;

  else :
   // no layouts found
  endif;
?>