admin管理员组

文章数量:1026640

I have a sub field that have several different values. What I'm trying to do is count the number of rows if they're equal to Mayor.

So my code looks like this:

if( have_rows('candidates') ): 
  while( have_rows('candidates') ): the_row(); 
    $cPosition = get_sub_field('candidate_position');
    if($cPosition == "Mayor"){
      $numrows = count( get_sub_field( 'candidate_position' ) );
    };
  endwhile;
endif;

echo $numrows;

However, I'm getting back ALL the rows. So like 17 instead of 8 or so.

Here is the loop giving me my results:

<div class="ballot-box ballot-box--mayor">
      <h3 class=""><?php echo _e('Mayor', 'HTML5Blank') ?></h3>

      <p>
        <?php echo _e('The Mayor of San Antonio works closely with City Council to pass laws and create policies for the City, as well as the City Manager, who serves as the City’s chief administrator. While City Council members are elected by district, the Mayor represents all of San Antonio (for a two-year term, with a four-term limit). Candidates are listed here as they will appear on the ballot. All information shared is public information.', 'HTML5Blank') ?>
      </p>
      <p class="text-med"><strong>
        <?php echo _e('Candidates:', 'HTML5Blank') ?> 
        <?php  
          if (have_rows('candidates')):
            $total = 0;
            while (have_rows('candidates')): the_row(); 
              $position = get_sub_field('candidate_position');
              if ($position == "Mayor") {
                $total++;
              };
            endwhile;
            echo $total;
          endif;
        ?>

        </strong></p>
      <p><a class="show-hide" role="button" data-toggle="collapse" href="#mayorCollapse" aria-expanded="false" aria-controls="mayorCollapse"><i class="fa fa-caret-down text-med" aria-hidden="true"></i> 
        <span class="toggle-text"><?php echo _e('Show:', 'HTML5Blank') ?> </span> <?php echo _e('Candidates', 'HTML5Blank') ?> </a></p>

      <div class="collapse" id="mayorCollapse">
        <?php 
            if( have_rows('candidates') ): 
        ?>
          <div class="option-box option-box--candidate">
            <?php 
              while( have_rows('candidates') ): the_row(); 
                $cPosition = get_sub_field('candidate_position');
                $cName     = get_sub_field('candidate_name');
                $cImage    = get_sub_field('candidate_photo');
                $cWebsite  = get_sub_field('candidate_website');
                $cPhone    = get_sub_field('candidate_phone');
                $cFB       = get_sub_field('candidate_facebook');
                $cTW       = get_sub_field('candidate_twitter');
                $cIG       = get_sub_field('candidate_instagram');

                if($cPosition == "Mayor"){
            ?>
            <div class="row">
              <div class="col-sm-3">
                <?php if($cImage){ ?>
                  <img class="responsive" src="<?php echo $cImage ?>" alt="<?php echo $cName; ?>" />
                <?php }else{ ?>
                  <img class="responsive mobile-hide" src="<?php echo get_stylesheet_directory_uri(); ?>/images/no-photo.png" alt="no photo provided" />
                <?php } ?>
              </div>

              <div class="col-sm-9">
                <h4 class=""><?php echo $cName; ?></h4>
                <div class="">
                  <?php if($cWebsite){ ?>
                    <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="<?php echo $cWebsite ?>" title="view website" onclick="trackOutboundLink('<?php echo $cWebsite; ?>', true, '<?php echo $cName; ?> Website');">Website</a>
                  <?php } ?>
                 <!--  Fililng document
                  <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="{{ candidate.filing }}" title="view filing document" onclick="trackOutboundLink('{{ candidate.filing }}', true, '<?php echo $cName; ?> Filing');">Filing Document</a>
                -->
                </div>

                <div class="social-icons">
                  <?php if($cFB){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cFB; ?>" title="view facebook page">
                      <i class="fa fa-facebook-official" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cTW){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cTW; ?>" title="view twitter page">
                      <i class="fa fa-twitter-square" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cIG){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cIG; ?>" title="view instagram">
                      <i class="fa fa-instagram" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                </div>

              </div>
            </div>
            <?php }; //end if ?>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
      </div>
    </div><!--./ballot-box -->

Any and all help is appreciated.

I have a sub field that have several different values. What I'm trying to do is count the number of rows if they're equal to Mayor.

So my code looks like this:

if( have_rows('candidates') ): 
  while( have_rows('candidates') ): the_row(); 
    $cPosition = get_sub_field('candidate_position');
    if($cPosition == "Mayor"){
      $numrows = count( get_sub_field( 'candidate_position' ) );
    };
  endwhile;
endif;

echo $numrows;

However, I'm getting back ALL the rows. So like 17 instead of 8 or so.

Here is the loop giving me my results:

<div class="ballot-box ballot-box--mayor">
      <h3 class=""><?php echo _e('Mayor', 'HTML5Blank') ?></h3>

      <p>
        <?php echo _e('The Mayor of San Antonio works closely with City Council to pass laws and create policies for the City, as well as the City Manager, who serves as the City’s chief administrator. While City Council members are elected by district, the Mayor represents all of San Antonio (for a two-year term, with a four-term limit). Candidates are listed here as they will appear on the ballot. All information shared is public information.', 'HTML5Blank') ?>
      </p>
      <p class="text-med"><strong>
        <?php echo _e('Candidates:', 'HTML5Blank') ?> 
        <?php  
          if (have_rows('candidates')):
            $total = 0;
            while (have_rows('candidates')): the_row(); 
              $position = get_sub_field('candidate_position');
              if ($position == "Mayor") {
                $total++;
              };
            endwhile;
            echo $total;
          endif;
        ?>

        </strong></p>
      <p><a class="show-hide" role="button" data-toggle="collapse" href="#mayorCollapse" aria-expanded="false" aria-controls="mayorCollapse"><i class="fa fa-caret-down text-med" aria-hidden="true"></i> 
        <span class="toggle-text"><?php echo _e('Show:', 'HTML5Blank') ?> </span> <?php echo _e('Candidates', 'HTML5Blank') ?> </a></p>

      <div class="collapse" id="mayorCollapse">
        <?php 
            if( have_rows('candidates') ): 
        ?>
          <div class="option-box option-box--candidate">
            <?php 
              while( have_rows('candidates') ): the_row(); 
                $cPosition = get_sub_field('candidate_position');
                $cName     = get_sub_field('candidate_name');
                $cImage    = get_sub_field('candidate_photo');
                $cWebsite  = get_sub_field('candidate_website');
                $cPhone    = get_sub_field('candidate_phone');
                $cFB       = get_sub_field('candidate_facebook');
                $cTW       = get_sub_field('candidate_twitter');
                $cIG       = get_sub_field('candidate_instagram');

                if($cPosition == "Mayor"){
            ?>
            <div class="row">
              <div class="col-sm-3">
                <?php if($cImage){ ?>
                  <img class="responsive" src="<?php echo $cImage ?>" alt="<?php echo $cName; ?>" />
                <?php }else{ ?>
                  <img class="responsive mobile-hide" src="<?php echo get_stylesheet_directory_uri(); ?>/images/no-photo.png" alt="no photo provided" />
                <?php } ?>
              </div>

              <div class="col-sm-9">
                <h4 class=""><?php echo $cName; ?></h4>
                <div class="">
                  <?php if($cWebsite){ ?>
                    <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="<?php echo $cWebsite ?>" title="view website" onclick="trackOutboundLink('<?php echo $cWebsite; ?>', true, '<?php echo $cName; ?> Website');">Website</a>
                  <?php } ?>
                 <!--  Fililng document
                  <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="{{ candidate.filing }}" title="view filing document" onclick="trackOutboundLink('{{ candidate.filing }}', true, '<?php echo $cName; ?> Filing');">Filing Document</a>
                -->
                </div>

                <div class="social-icons">
                  <?php if($cFB){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cFB; ?>" title="view facebook page">
                      <i class="fa fa-facebook-official" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cTW){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cTW; ?>" title="view twitter page">
                      <i class="fa fa-twitter-square" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cIG){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cIG; ?>" title="view instagram">
                      <i class="fa fa-instagram" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                </div>

              </div>
            </div>
            <?php }; //end if ?>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
      </div>
    </div><!--./ballot-box -->

Any and all help is appreciated.

Share Improve this question edited Mar 28, 2019 at 17:10 Lz430 asked Mar 28, 2019 at 14:45 Lz430Lz430 1378 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

PHP's count() function is generally used to count elements in an array, but you're counting the value of a string, and that always returns 1. You can just add 1 in your if statement.

Something like this:

if (have_rows('candidates')):
  $total = 0;
  while (have_rows('candidates')): the_row(); 
    $position = get_sub_field('candidate_position');
    if ($position == "Mayor") {
      $total++;
    };
  endwhile;
  echo $total;
endif;

Other than that, the code actually looks right. Where are you initializing $numrows and where are you checking the value? Do you get a different value using the code above?

I have a sub field that have several different values. What I'm trying to do is count the number of rows if they're equal to Mayor.

So my code looks like this:

if( have_rows('candidates') ): 
  while( have_rows('candidates') ): the_row(); 
    $cPosition = get_sub_field('candidate_position');
    if($cPosition == "Mayor"){
      $numrows = count( get_sub_field( 'candidate_position' ) );
    };
  endwhile;
endif;

echo $numrows;

However, I'm getting back ALL the rows. So like 17 instead of 8 or so.

Here is the loop giving me my results:

<div class="ballot-box ballot-box--mayor">
      <h3 class=""><?php echo _e('Mayor', 'HTML5Blank') ?></h3>

      <p>
        <?php echo _e('The Mayor of San Antonio works closely with City Council to pass laws and create policies for the City, as well as the City Manager, who serves as the City’s chief administrator. While City Council members are elected by district, the Mayor represents all of San Antonio (for a two-year term, with a four-term limit). Candidates are listed here as they will appear on the ballot. All information shared is public information.', 'HTML5Blank') ?>
      </p>
      <p class="text-med"><strong>
        <?php echo _e('Candidates:', 'HTML5Blank') ?> 
        <?php  
          if (have_rows('candidates')):
            $total = 0;
            while (have_rows('candidates')): the_row(); 
              $position = get_sub_field('candidate_position');
              if ($position == "Mayor") {
                $total++;
              };
            endwhile;
            echo $total;
          endif;
        ?>

        </strong></p>
      <p><a class="show-hide" role="button" data-toggle="collapse" href="#mayorCollapse" aria-expanded="false" aria-controls="mayorCollapse"><i class="fa fa-caret-down text-med" aria-hidden="true"></i> 
        <span class="toggle-text"><?php echo _e('Show:', 'HTML5Blank') ?> </span> <?php echo _e('Candidates', 'HTML5Blank') ?> </a></p>

      <div class="collapse" id="mayorCollapse">
        <?php 
            if( have_rows('candidates') ): 
        ?>
          <div class="option-box option-box--candidate">
            <?php 
              while( have_rows('candidates') ): the_row(); 
                $cPosition = get_sub_field('candidate_position');
                $cName     = get_sub_field('candidate_name');
                $cImage    = get_sub_field('candidate_photo');
                $cWebsite  = get_sub_field('candidate_website');
                $cPhone    = get_sub_field('candidate_phone');
                $cFB       = get_sub_field('candidate_facebook');
                $cTW       = get_sub_field('candidate_twitter');
                $cIG       = get_sub_field('candidate_instagram');

                if($cPosition == "Mayor"){
            ?>
            <div class="row">
              <div class="col-sm-3">
                <?php if($cImage){ ?>
                  <img class="responsive" src="<?php echo $cImage ?>" alt="<?php echo $cName; ?>" />
                <?php }else{ ?>
                  <img class="responsive mobile-hide" src="<?php echo get_stylesheet_directory_uri(); ?>/images/no-photo.png" alt="no photo provided" />
                <?php } ?>
              </div>

              <div class="col-sm-9">
                <h4 class=""><?php echo $cName; ?></h4>
                <div class="">
                  <?php if($cWebsite){ ?>
                    <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="<?php echo $cWebsite ?>" title="view website" onclick="trackOutboundLink('<?php echo $cWebsite; ?>', true, '<?php echo $cName; ?> Website');">Website</a>
                  <?php } ?>
                 <!--  Fililng document
                  <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="{{ candidate.filing }}" title="view filing document" onclick="trackOutboundLink('{{ candidate.filing }}', true, '<?php echo $cName; ?> Filing');">Filing Document</a>
                -->
                </div>

                <div class="social-icons">
                  <?php if($cFB){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cFB; ?>" title="view facebook page">
                      <i class="fa fa-facebook-official" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cTW){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cTW; ?>" title="view twitter page">
                      <i class="fa fa-twitter-square" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cIG){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cIG; ?>" title="view instagram">
                      <i class="fa fa-instagram" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                </div>

              </div>
            </div>
            <?php }; //end if ?>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
      </div>
    </div><!--./ballot-box -->

Any and all help is appreciated.

I have a sub field that have several different values. What I'm trying to do is count the number of rows if they're equal to Mayor.

So my code looks like this:

if( have_rows('candidates') ): 
  while( have_rows('candidates') ): the_row(); 
    $cPosition = get_sub_field('candidate_position');
    if($cPosition == "Mayor"){
      $numrows = count( get_sub_field( 'candidate_position' ) );
    };
  endwhile;
endif;

echo $numrows;

However, I'm getting back ALL the rows. So like 17 instead of 8 or so.

Here is the loop giving me my results:

<div class="ballot-box ballot-box--mayor">
      <h3 class=""><?php echo _e('Mayor', 'HTML5Blank') ?></h3>

      <p>
        <?php echo _e('The Mayor of San Antonio works closely with City Council to pass laws and create policies for the City, as well as the City Manager, who serves as the City’s chief administrator. While City Council members are elected by district, the Mayor represents all of San Antonio (for a two-year term, with a four-term limit). Candidates are listed here as they will appear on the ballot. All information shared is public information.', 'HTML5Blank') ?>
      </p>
      <p class="text-med"><strong>
        <?php echo _e('Candidates:', 'HTML5Blank') ?> 
        <?php  
          if (have_rows('candidates')):
            $total = 0;
            while (have_rows('candidates')): the_row(); 
              $position = get_sub_field('candidate_position');
              if ($position == "Mayor") {
                $total++;
              };
            endwhile;
            echo $total;
          endif;
        ?>

        </strong></p>
      <p><a class="show-hide" role="button" data-toggle="collapse" href="#mayorCollapse" aria-expanded="false" aria-controls="mayorCollapse"><i class="fa fa-caret-down text-med" aria-hidden="true"></i> 
        <span class="toggle-text"><?php echo _e('Show:', 'HTML5Blank') ?> </span> <?php echo _e('Candidates', 'HTML5Blank') ?> </a></p>

      <div class="collapse" id="mayorCollapse">
        <?php 
            if( have_rows('candidates') ): 
        ?>
          <div class="option-box option-box--candidate">
            <?php 
              while( have_rows('candidates') ): the_row(); 
                $cPosition = get_sub_field('candidate_position');
                $cName     = get_sub_field('candidate_name');
                $cImage    = get_sub_field('candidate_photo');
                $cWebsite  = get_sub_field('candidate_website');
                $cPhone    = get_sub_field('candidate_phone');
                $cFB       = get_sub_field('candidate_facebook');
                $cTW       = get_sub_field('candidate_twitter');
                $cIG       = get_sub_field('candidate_instagram');

                if($cPosition == "Mayor"){
            ?>
            <div class="row">
              <div class="col-sm-3">
                <?php if($cImage){ ?>
                  <img class="responsive" src="<?php echo $cImage ?>" alt="<?php echo $cName; ?>" />
                <?php }else{ ?>
                  <img class="responsive mobile-hide" src="<?php echo get_stylesheet_directory_uri(); ?>/images/no-photo.png" alt="no photo provided" />
                <?php } ?>
              </div>

              <div class="col-sm-9">
                <h4 class=""><?php echo $cName; ?></h4>
                <div class="">
                  <?php if($cWebsite){ ?>
                    <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="<?php echo $cWebsite ?>" title="view website" onclick="trackOutboundLink('<?php echo $cWebsite; ?>', true, '<?php echo $cName; ?> Website');">Website</a>
                  <?php } ?>
                 <!--  Fililng document
                  <a class="btn btn-sm btn-sa-dark-aqua" target="_blank" href="{{ candidate.filing }}" title="view filing document" onclick="trackOutboundLink('{{ candidate.filing }}', true, '<?php echo $cName; ?> Filing');">Filing Document</a>
                -->
                </div>

                <div class="social-icons">
                  <?php if($cFB){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cFB; ?>" title="view facebook page">
                      <i class="fa fa-facebook-official" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cTW){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cTW; ?>" title="view twitter page">
                      <i class="fa fa-twitter-square" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                  <?php if($cIG){ ?>
                    <a class="text-xl nounderline" target="_blank" href="<?php echo $cIG; ?>" title="view instagram">
                      <i class="fa fa-instagram" aria-hidden="true"></i>
                    </a>
                  <?php } ?>
                </div>

              </div>
            </div>
            <?php }; //end if ?>
            <?php endwhile; ?>
          </div>
        <?php endif; ?>
      </div>
    </div><!--./ballot-box -->

Any and all help is appreciated.

Share Improve this question edited Mar 28, 2019 at 17:10 Lz430 asked Mar 28, 2019 at 14:45 Lz430Lz430 1378 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

PHP's count() function is generally used to count elements in an array, but you're counting the value of a string, and that always returns 1. You can just add 1 in your if statement.

Something like this:

if (have_rows('candidates')):
  $total = 0;
  while (have_rows('candidates')): the_row(); 
    $position = get_sub_field('candidate_position');
    if ($position == "Mayor") {
      $total++;
    };
  endwhile;
  echo $total;
endif;

Other than that, the code actually looks right. Where are you initializing $numrows and where are you checking the value? Do you get a different value using the code above?

本文标签: phpHow can I count ACF subfield with a certain value