【问题标题】:ACF adding CPT fields to headerACF 将 CPT 字段添加到标题
【发布时间】:2018-06-18 21:43:54
【问题描述】:

我有一个名为“项目”的自定义帖子类型,并且我在我的 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 wordpress advanced-custom-fields


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 2019-07-14
      • 2023-04-05
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多