我使用以下代码来显示特定联赛的总年数,但每年显示1个。
例如,西班牙语Primera Division有三年但显示1,1,1而不是3。
我使用了count($object);
和sizeof($object);
,但没有得到理想的结果。
这是我的代码:
<div class=" reds clearfix"; style="width: 50%;">
<ul id="test">
<!-- the loop -->
<?php while ( $fixture_query->have_posts() ) : $fixture_query->the_post(); ?>
<?php
$champion_team = get_post_meta( get_the_ID(), 'football_league_team_name', true );
$terms_competition = get_the_terms( get_the_ID(), 'competition' );
$terms_session = get_the_terms( get_the_ID(), 'session' ); ?>
<?php foreach ($terms_session as $object) {?>
<li style="float: left; margin-right: 5px;"> <?php echo $object->name; ?></li>
<?php
//echo count($object); // output 2
echo sizeof($object);
// echo count($object->name);
?>
<?php } ?>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</ul>
</div>
答案 0 :(得分:0)
<?php
echo count($terms_session);//this give all count of elements
foreach ($terms_session as $object) { ?>
<li style="float: left; margin-right: 5px;"> <?php echo $object->name; ?></li>
<?php
//echo count($object); // output 2
echo sizeof($object);
// echo count($object->name);
?>
<?php }
?>
答案 1 :(得分:0)
我假设您要显示foreach循环内的会话总数。
根据您的代码,您可以将会话总数计算为
$total_sessions = count($terms_session);
现在您可以在foreach循环中轻松使用此变量
<?php
foreach ($terms_session as $object) {?>
<li style="float: left; margin-right: 5px;"> <?php echo $object->name; ?></li>
<?php
echo $total_sessions;
?>
<?php }
?>
以下是包含更改的完整代码,请参阅添加内容的注释。
<div class=" reds clearfix"; style="width: 50%;">
<ul id="test">
<!-- the loop -->
<?php while ( $fixture_query->have_posts() ) : $fixture_query->the_post(); ?>
<?php
$champion_team = get_post_meta( get_the_ID(), 'football_league_team_name', true );
$terms_competition = get_the_terms( get_the_ID(), 'competition' );
$terms_session = get_the_terms( get_the_ID(), 'session' );
// Calculate the total number of sessions
$total_sessions = count($terms_session);
?>
<?php
foreach ($terms_session as $object) {?>
<li style="float: left; margin-right: 5px;"> <?php echo $object->name; ?></li>
<?php
// Display the total number of sessions
echo $total_sessions;
?>
<?php }
?>
<?php endwhile; ?>
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</ul>
</div>