[英]PHP to loop a list presented in a table - I cannot understand how it works
这是我第一次修改其他人的WordPress主题,我无法弄清楚这段代码是如何工作的。
以下代码在具有3列和无限行的表中生成列表。 此代码如何定义列数? 如何将3列更改为5列,依此类推?
<table class="brands">
<tbody>
<?php if($terms): ?>
<?php foreach($terms as $term): ?>
<tr>
<?php
foreach($term as $tr):
$term_name = $tr->name;
$term_id = $tr->term_id;
$term_thumb = get_field("thumbnail","{$listing_cat}_{$term_id}");
$term_link = get_term_link($tr->slug,$listing_cat);
?>
<td>
<a href="<?php echo $term_link; ?>">
<?php if($term_thumb): ?>
<img src="<?php echo $term_thumb["url"]; ?>" alt="<?php echo $term_name; ?>">
<?php endif; ?>
<p><?php echo $term_name; ?></p>
</a>
</td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
尝试,
<?php
print_r($term);
foreach($term as $tr):
您可能会注意到count($ term)= 3,这就是为什么它要打印3列。 好吧,要获得更多列,请尝试在$ term中推送更多元素。
至于“无限行”,这取决于$ terms数组的长度。
它显示3列,因为主题数据库中没有默认$ terms。 它为每个$ item运行一个循环,因此,基本上,要运行5列,您将不得不将项目推入数据库表,或者可以跳过检查$ item并以以下方式运行循环
<table class="brands">
<tbody>
<?php for(i=0; i<5, i++){ ?>
<tr>
<?php
//what you want to display
?>
<td>
//your table data
</td>
</tr>
<?php }?>
</tbody>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.