我有一个子字段,它有几个不同的值。我要做的是计算行数,如果它们等于Mayor
.
因此,我的代码如下所示:
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;
然而,我要收回所有的行。大概是17岁,而不是8岁左右。
下面是给出我的结果的循环:
<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 -->
非常感谢您的任何帮助。
最合适的回答,由SO网友:NightHawk 整理而成
PHP的count()
函数通常用于计算数组中的元素,但您计算的是字符串的值,它总是返回1。只需在if语句中添加1即可。
类似这样:
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;
除此之外,代码实际上看起来是正确的。您在哪里初始化
$numrows
您在哪里检查值?您是否使用上述代码获得不同的值?