Support » Plugins » Hacks » How to split up output from foreach get_categories() loop

  • Hi, i’m using a plugin to attach an image to each category in wp and i’m using this code from the plugin coder to display my categories:

    <?php foreach (get_categories() as $cat) : ?>
    <p align=center><a href="<?php echo get_category_link($cat->term_id); ?>"> <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
    <br><a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a></p><br><br>
    <?php endforeach; ?>

    However by now i have over 150 categories and they all get listed with their image into one gigantic page with over 20mb of data. I’m not a coder myself and tried the last days to come up with a solution but i failed.
    I’d like to split the output either by alphabet (ie. list A-F, G-L and so on) or by pages with for example 25 categories listed on each page.
    Maybe someone can give me a hint how to do that in php?

Viewing 1 replies (of 1 total)
  • Moderator bcworkz


    I’m not sure you can use get_categories() to return a limited alphabetical range, but it will return a limited number and offset such as 25 categories starting at #50. So you could simply have links to pages 1, 2, 3 etc. and calculate the offset required for each page.

    To have a limited alphabetic range, the actual HTML output could be inside a PHP if() conditional so that only category names matching a particular regexp are actually output even though you are looping through all the categories.

Viewing 1 replies (of 1 total)
  • The topic ‘How to split up output from foreach get_categories() loop’ is closed to new replies.