设置多个类别以显示在页面上
Set up a number of categories to display on a page
我基本上是在寻找一种在我的模板页面上只显示 3 个类别的方法。
我使用 foreach 循环 来显示我页面上的所有类别
$categories = get_categories();
foreach($categories as $category) {
echo '<div class="col-md-4"><a href="' .
get_category_link($category->term_id) . '">' . $category->name
. '</a></div>';
}?>
此代码显示了我页面上的所有类别,但我只想每页显示 3 个类别并最终添加分页
根据我上面的评论。我可以给你的代码示例如下
for($i = 0; $i <= count($categories); $i++) {
/*
Pagination purposes this is how you can format data to an object
base.
*/
if($i % 3 = 0) {
// add to an array
}
}
for ($i = 0; $i <= 3; $i++) {
// echo categories HTML but $categories[$i] instead
}
虽然让大家知道这不会像您想象的那样完全工作等等......我无法深入解释模数运算符因为如果你不尝试学习它我想不出一个简单的方法所以你可以很好地理解它。
这样你只能得到 3
$page=$_GET['page_number'];
$categories = get_categories();
$data=array_chunk($categories ,3);
$paginations=count($data); // use loop for paginations in html page
foreach($data[$page] as $category) {
echo '<div class="col-md-4"><a href="' .
get_category_link($category->term_id) . '">' . $category->name
. '</a></div>';
}
$startCategory = 0;
for($i = $startCategory; $i <= count($categories); $i++) {
echo '<div class="col-md-4"><a href="' .get_category_link($categories[$i]['term_id']) . '">' . $categories[$i]['name']. '</a></div>';
$startCategory++;
if($startCategory > (count($categories) -1)){
break;
}
//pass $startCategory to next page , next page
$startCategory = $_POST["startCategory"]; // by using post method
$startCategory = $_GET["startCategory"]; // by using get method
for($i = $startCategory; $i <= count($categories); $i++) {
echo '<div class="col-md-4"><a href="' .
get_category_link($categories[$i]['term_id']) . '">' . $categories[$i]['name']
. '</a></div>';
$startCategory++;
if($startCategory > (count($categories) -1)){
break;
}
}
我基本上是在寻找一种在我的模板页面上只显示 3 个类别的方法。 我使用 foreach 循环 来显示我页面上的所有类别
$categories = get_categories();
foreach($categories as $category) {
echo '<div class="col-md-4"><a href="' .
get_category_link($category->term_id) . '">' . $category->name
. '</a></div>';
}?>
此代码显示了我页面上的所有类别,但我只想每页显示 3 个类别并最终添加分页
根据我上面的评论。我可以给你的代码示例如下
for($i = 0; $i <= count($categories); $i++) {
/*
Pagination purposes this is how you can format data to an object
base.
*/
if($i % 3 = 0) {
// add to an array
}
}
for ($i = 0; $i <= 3; $i++) {
// echo categories HTML but $categories[$i] instead
}
虽然让大家知道这不会像您想象的那样完全工作等等......我无法深入解释模数运算符因为如果你不尝试学习它我想不出一个简单的方法所以你可以很好地理解它。
这样你只能得到 3
$page=$_GET['page_number'];
$categories = get_categories();
$data=array_chunk($categories ,3);
$paginations=count($data); // use loop for paginations in html page
foreach($data[$page] as $category) {
echo '<div class="col-md-4"><a href="' .
get_category_link($category->term_id) . '">' . $category->name
. '</a></div>';
}
$startCategory = 0;
for($i = $startCategory; $i <= count($categories); $i++) {
echo '<div class="col-md-4"><a href="' .get_category_link($categories[$i]['term_id']) . '">' . $categories[$i]['name']. '</a></div>';
$startCategory++;
if($startCategory > (count($categories) -1)){
break;
}
//pass $startCategory to next page , next page
$startCategory = $_POST["startCategory"]; // by using post method
$startCategory = $_GET["startCategory"]; // by using get method
for($i = $startCategory; $i <= count($categories); $i++) {
echo '<div class="col-md-4"><a href="' .
get_category_link($categories[$i]['term_id']) . '">' . $categories[$i]['name']
. '</a></div>';
$startCategory++;
if($startCategory > (count($categories) -1)){
break;
}
}