ACF:当自定义字段(在字段组中)为空时隐藏div或模板节

时间:2019-06-22 作者:festgebaeck

当前,我正在尝试在后端的自定义字段未填充时隐藏或不显示模板的div或部分。问题是,对于未嵌套的单个自定义字段有效的方法,对于父组中有一个或两个子组的嵌套/分组自定义字段无效。

下面是带有自定义字段显示的代码。显示器本身工作正常。我只是想把一切都隐藏起来<section> 当ACF组的一个子字段未填写时。该组由一个父组、三个子组组成,每个子组有几个字段。

<?php
      $werdegang = get_field(\'werdegang\'); // werdegang parent group
      $station1 = $werdegang[\'station_1\']; // \' child group 1
      $station2 = $werdegang[\'station_2\']; // \'child group 2
      $station3 = $werdegang[\'station_3\']; // \'child group 3

   ?>

  <section class="werdegang">

    <span class="werdegang-headline">Headline</span>
    <?php the_sub_field(\'jahr_station_1\'); ?>
    <hr class="willkommen-divider"></hr>

    <div class="werdegang-container">
      <div class="timeline">
        <div class="timeline-nav">
          <div class="timeline-nav__item"><?php echo $station1[\'jahr_station_1\'];?></div>
          <div class="timeline-nav__item"><?php echo $station2[\'jahr_station_2\'];?></div>
          <div class="timeline-nav__item"><?php echo $station3[\'jahr_station_3\'];?></div>
        </div>
        <div class="timeline-wrapper">
          <div class="timeline-slider">
            <div class="timeline-slide" data-year="<?php echo $station1[\'jahr_station_1\'];?>">
              <div class="timeline-slide__content">
                <h4 class="timeline-title"><?php echo $station1[\'titel_station_1\'];?></h4>
                <p class="timeline-text"><?php echo $station1[\'beschreibung_station_1\'];?></p>
              </div>
            </div>
            <div class="timeline-slide" data-year="<?php echo $station2[\'jahr_station_2\'];?>">
              <div class="timeline-slide__content">
                <h4 class="timeline-title"><?php echo $station2[\'titel_station_2\'];?></h4>
                <p class="timeline-text"><?php echo $station2[\'beschreibung_station_2\'];?></p>
              </div>
            </div>
            <div class="timeline-slide"  data-year="<?php echo $station3[\'jahr_station_3\'];?>">
              <div class="timeline-slide__content">
                <h4 class="timeline-title"><?php echo $station3[\'titel_station_3\'];?></h4>
                <p class="timeline-text"><?php echo $station3[\'beschreibung_station_3\'];?></p>
              </div>
            </div>

          </div>
        </div>
      </div>
    </div>


      <?php if( get_field(\'file\') ): ?>
      <span class="lebenslauf-button profil-button"><a class="lebenslauf-link" href="<?php the_field(\'file\'); ?>"> <span>Vollständigen Lebenslauf ansehen</span></a></span>
    <?php endif; ?>


  </section>
到目前为止我所做的尝试:

<?php
if ($station1[\'jahr_station_1\'])) {
?>
  SECTION CONTENT

<?php
}
  ?>
以及:

<?php
 $conditionalw = get_sub_field(\'jahr_station_1\');
?>
<?php if ( $conditionalw ) : ?>
SECTION CONTENT
<?php endif; ?>
以及:

 <?php
if( $werdegang ):
 ?>
SECTION CONTENT
<?php endif; ?>
但我似乎无法找到正确的组合来隐藏该部分。在尝试上述代码片段时,我隐藏了与是否填写自定义字段无关的部分。对于只有一行子字段的组,它至少会显示,但在未填写时,仍然无法隐藏整个div或section。

也许有人提供了一些指针,可以帮助我找出在ACF中没有填写嵌套组时如何隐藏div或section。

1 个回复
SO网友:Phosl

希望这有助于了解您的请求

<?php 
$werdegang = \'werdegang\'; // werdegang parent group
$station1 = \'jahr_station_1\'; // \' child group 1
$station2 = \'jahr_station_2\'; // \'child group 2
$station3 = \'jahr_station_3\'; // \'child group 3
$station1Jahr = \'jahr1\';
$station2Jahr = \'jahr2\';
$station3Jahr = \'jahr3\';
$station1Title = \'jahr1_Title\';
$station1Text = \'jahr1_Text\';
$station2Title = \'jahr2_Title\';
$station2Text = \'jahr2_Text\';
$station3Title = \'jahr3_Title\';
$station3Text = \'jahr3_Text\';   
?>
<?php if(   
        (!empty($station1Title) && !empty($station1Text)) 
        && (!empty($station2Title) && !empty($station2Text)) 
        && (!empty($station3Title) && !empty($station3Text)) 
        && (!empty($station1) && !empty($station2) && !empty($station3))  
        && (!empty($station1Jahr) && !empty($station2Jahr) && !empty($station3Jahr))  
    ) { ?>
<section class="werdegang"> 
<span class="werdegang-headline">Headline</span>
<?php echo $station1; ?>
<hr class="willkommen-divider"></hr>
<div class="werdegang-container">
<div class="timeline">
    <div class="timeline-nav">
      <div class="timeline-nav__item"><?php echo $station1Jahr;?></div>
      <div class="timeline-nav__item"><?php echo $station2Jahr;?></div>
      <div class="timeline-nav__item"><?php echo $station3Jahr;?></div>
    </div>
        <div class="timeline-wrapper">
          <div class="timeline-slider">
            <?php if(  !empty($station1Title) && !empty($station1Text) ) { ?>
                <div class="timeline-slide" data-year="<?php echo $station1Jahr;?>">
                  <div class="timeline-slide__content">
                    <h4 class="timeline-title"><?php echo $station1Title;?></h4>
                    <p class="timeline-text"><?php echo $station1Text;?></p>
                  </div>
                </div>
            <?php }?>
            <?php if(  !empty($station2Title) && !empty($station2Text) ) { ?>
                <div class="timeline-slide" data-year="<?php echo $station2Jahr;?>">
                  <div class="timeline-slide__content">
                    <h4 class="timeline-title"><?php echo $station2Title;?></h4>
                    <p class="timeline-text"><?php echo $station2Text;?></p>
                  </div>
                </div>
            <?php }?>
            <?php if(  !empty($station3Title) && !empty($station3Text) ) { ?>
                <div class="timeline-slide" data-year="<?php echo $station3Jahr;?>">
                  <div class="timeline-slide__content">
                    <h4 class="timeline-title"><?php echo $station3Title;?></h4>
                    <p class="timeline-text"><?php echo $station3Text;?></p>
                  </div>
                </div>
               <?php }?>

              </div>
              </div>
              </div>
              </div>
              </section>


              <?php } else {
   echo \'some field are not filled\';
 }
 ?>