当子div不存在时隐藏div

时间:2018-08-10 19:47:57

标签: javascript php

函数运行时每次都拉标题“ Founders”。我希望它起作用,当<?php if (get_field('choose_a_level')=='Founders'){ ?>不返回任何帖子时,h3标签中的主要标题将不会显示。

我使用了创始者类来定位帖子。我尝试过

$('.team-header').not($('.founder').parent().parent()).hide();

我已经花了几个小时的时间,反复试验,非常感谢您的帮助。

谢谢你,艾尔

function alec_cc_render_team_members_founders() {
    $args = array(
        'numberposts' => -1,
        'post_type' => 'team_members'
    );

    $team_members = new WP_Query($args);
    $count = 0;
 if( $team_members->have_posts() ): ?>

 <div class="clear"></div>
 <h3 class="team-header">Founders</h2>

    <div class="gdlr-personnel-item-wrapper team-grid">
    <div class="clear"></div>
<?php


    while( $team_members->have_posts() ) :
        $team_members->the_post();
        $count++;
    ?>
  <?php if (get_field('choose_a_level')=='Founders'){ ?>
    <div class="four columns" onresize="founderResize()">
        <div class="gdlr-item founder gdlr-personnel-item plain-style gdlr-left">
            <div class="gdlr-ux gdlr-personnel-ux">
            </div>
        </div>
   <?php
   if ($count == 4) {
    $count = 0;
    ?>
        <div class="clear"></div>
   <?php
    }
    ?>
    </div>
<?php } ?>

<?php endwhile;
        endif;
    ?>
  </div>
    <script type="text/javascript">
  function founderResize() {
      var maxHeight = 0;

      var elements = document.getElementsByClassName('founder');

      var elementHeights = Array.prototype.map.call(elements, function(el)  {
        return el.clientHeight;
      });

      var maxHeight = Math.max.apply(null, elementHeights);

      // surround this in a function

      Array.prototype.forEach.call(elements, function(el) {
      el.style.height = maxHeight + "px"
      });

      // Call the function a half second after the page loads
      // Call the function when the window resizes


  }

    </script>
    <?php
}

add_shortcode('team_members_founders', 'alec_cc_render_team_members_founders');

1 个答案:

答案 0 :(得分:0)

<div class="gdlr-item founder gdlr-personnel-item plain-style gdlr-left">不是<h3 class="team-header">的子级,因此您要执行的测试无效。

您可以在PHP中有条件地输出标题,而不是使用Javascript修复此问题。在第一次输出创始人之前,在循环中进行操作。

    $first_founder = true;
    while( $team_members->have_posts() ) :
        $team_members->the_post();
        $count++;
    ?>
  <?php if (get_field('choose_a_level')=='Founders'){ ?>
    if ($first_founder) { 
     $first_founder = false; ?>
     <div class="clear"></div>
     <h3 class="team-header">Founders</h3>

        <div class="gdlr-personnel-item-wrapper team-grid">
        <div class="clear"></div>
    <?php }
    <div class="four columns" onresize="founderResize()">
        <div class="gdlr-item founder gdlr-personnel-item plain-style gdlr-left">
            <div class="gdlr-ux gdlr-personnel-ux">
            </div>
        </div>
   <?php
   if ($count == 4) {
    $count = 0;
    ?>
        <div class="clear"></div>
   <?php
    }
    if (!$first_founder) { ?>
    </div>
    <?php }
    ?>