代码之家  ›  专栏  ›  技术社区  ›  Duggy G

ACF向标头添加CPT字段

  •  0
  • Duggy G  · 技术社区  · 8 年前

    我有一个名为“项目”的自定义帖子类型,并且我在我的单个项目中添加了灵活的字段内容。php。我试图为主标题中的每个项目添加一个英雄形象。php,因为我希望我的英雄形象坐在导航后面。我有一切工作,但我的英雄形象是显示在CPT存档页面和CPT单页,但我只想在每个项目页面上显示英雄形象。

    在我的标题中。我拥有的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;
        ?>
    
    1 回复  |  直到 8 年前
        1
  •  0
  •   Nate Beers    8 年前

    也许您可以尝试仅在单个页面上显示:

    <?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;
    ?>