admin管理员组文章数量:1122846
I am looking for help in how to paginate my foreach output. I've looked at other questions and answers and cannot find a solution that works for me or that I can figure out on my own. Right now my code, which is below, outputs everything into table rows. My problem, of course, is that it dumps all data on a single page -- hence the reason I want pagination. I want to paginate for every 11 items on the page. The page is a magazine archive, and there are 11 issues pear year -- so every page is equal to 1 year of our magazine. The first page should host issues 1-11 and page two should host issues 12 through 22, etc. We have 10 years worth of magazine issues. Any help would be greatly appreciated. Thank you!
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>
<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>
I am looking for help in how to paginate my foreach output. I've looked at other questions and answers and cannot find a solution that works for me or that I can figure out on my own. Right now my code, which is below, outputs everything into table rows. My problem, of course, is that it dumps all data on a single page -- hence the reason I want pagination. I want to paginate for every 11 items on the page. The page is a magazine archive, and there are 11 issues pear year -- so every page is equal to 1 year of our magazine. The first page should host issues 1-11 and page two should host issues 12 through 22, etc. We have 10 years worth of magazine issues. Any help would be greatly appreciated. Thank you!
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('term') as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>
<em><a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><?php echo $cat->description; ?></a></em><br>
<a href="<?php echo get_term_link($cat->slug, 'term'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>
Share
Improve this question
asked Nov 18, 2013 at 22:03
user2591811user2591811
391 silver badge6 bronze badges
1 Answer
Reset to default 0Here is the code I used to solve my problem:
<?php
$url = $_SERVER["REQUEST_URI"];
$segments = explode('/', $url);
$page = is_numeric($segments[count($segments)-2]) ? $segments[count($segments)-2] : 1;
$next = $page + 1;
$prev = $page - 1;
$issues_per_page = 11;
$lastpage = ceil(wp_count_terms( 'mag') / $issues_per_page) ;
?>
<?php wp_count_terms( 'mag' ); ?>
<table>
<tr>
<?php $col = 0; ?>
<?php foreach (get_terms('mag', array('offset' => ($page - 1) * $issues_per_page, 'number' => $issues_per_page)) as $cat) : ?>
<?php if ($col > 0 && $col % 3 == 0): ?>
</tr>
<tr>
<?php endif; ?>
<?php $col++; ?>
<td>
<a href="<?php echo get_term_link($cat->slug, 'mag'); ?>"><strong><?php echo $cat->name; ?></strong></a><br>
<em><a href="<?php echo get_term_link($cat->slug, 'mag'); ?>"><?php echo $cat->description; ?></a></em><br>
<a href="<?php echo get_term_link($cat->slug, 'mag'); ?>"><img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /></a>
</td>
<?php endforeach; ?>
</tr>
</table>
<?php if ($prev > 0) : ?>
<a href="/mag-archive?page=<?php echo $prev; ?>">Previous</a>
<?php endif ?>
<?php if ($page < $lastpage) : ?>
<a href="/mag-archive?page=<?php echo $next; ?>">Next</a>
<?php endif ?>
本文标签: phpforeach pagination
版权声明:本文标题:php - foreach pagination 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736280755a1926098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论