John Davidson

php - Set up a number of categories to display on a page

0 comments
Message:


I am basically looking for a way to display only 3 categories on my template page.
I used a foreach loop to display all the categories on my pages


$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>';
}?>

this code displays all the categories on my page but I just want to display 3 per page and eventually add paginations



My Answer:
To display only 3 categories on your page, you can modify the code to limit the number of categories displayed. Here's an example:

php
$categories = get_categories(array('number' => 3)); // Limit to 3 categories
foreach($categories as $category) {
echo '';
}


This code will only display 3 categories on your page. If you want to add pagination to display more categories, you can use the `paged` parameter in the `get_categories` function and set up pagination controls accordingly.

Rate this post

5 of 5 based on 3156 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us