How to create a “category loop”?
-
Just like how I would use
query_posts
to loop through posts, I would like to do that with categories instead. Showing the category title, description etc.So it’s basically a loop listing categories.
-
Did you look at wp_list_categories
I did, but from my point o f view it only left you create a list with links?
Put this function in functions.php
function show_categories($excl=''){ $categories = get_the_category($post->ID); if(!empty($categories)){ $exclude=$excl; $exclude = explode(",", $exclude); foreach ($categories as $cat) { if(!in_array($cat->cat_ID, $exclude)) { echo '<p>' . $cat->cat_name . '</p>'; echo '<p>' . $cat->category_description . '</p>'; } } } }
You can call this function inside the loop in your template by:
<?php show_categories('1,4,6'); ?>
the comma seperated list (‘1,4,6’) is a list of categories you want to exclude. if you don’t want to exlude use this:
<?php show_categories(); ?>
get_the_category is an array that produces these values[term_id] => 5 [name] => News [slug] => news [term_group] => 0 [term_taxonomy_id] => 5 [taxonomy] => category [description] => This is a news category [parent] => 0 [count] => 3 [object_id] => 114 [cat_ID] => 5 [category_count] => 3 [category_description] => This is a news category [cat_name] => News [category_nicename] => news [category_parent] => 0
so if you want the postcount in there you put this in the function:
echo '<p>' . $cat->count . '</p>';
and so on..Hey,
I’m having troubles with the standard wp_list_categories. For some reason it won’t “work” in one of my pages. (I’ve got Exec-PHP installed).
Do you think it’s worth trying the function above?
Can you access functions.php via the dashboard, or do I need to find the file manually?
Cheers
Just try the function and alter it the way you want. In the dashboard go to Appearance and then to Editor. You can edit the functions.php file there. It is listed under: Theme Functions (functions.php). This is if your theme has a functions.php templatefile
OK, thanks
Hey,
OK so I added the function to my functions.php file, no problems.
I’ve called the function, in a page, like this:
<p> Full category listing: </p> <ul> <?php show_categories(); ?> </ul>
It just doesn’t work, I don’t get it…
If someone could take a look at the two pages I’m talking about? This page here has two links at the very top, one to an archive page and the other to a category listing. One works, the other doesn’t:
http://www.afterbang.co.uk/links/
Any ideas why categories aren’t working?
ok I’m so sorry. Wrong function
here you go
function list_my_categories($exclude = '') { if (!empty($exclude)) { $exclude = 'exclude=' . $exclude; } $categories = get_categories($exclude); $html = ''; foreach ($categories as $cat) { if($cat->category_parent == 0) { $html .= '<p>' . $cat->cat_name . '</p>'; $html .= '<p>' . $cat->category_description . '</p>'; $childcategories = get_categories('child_of='.$cat->cat_ID.''); if(!empty($childcategories)){ foreach ($childcategories as $ccat) { $html .= '<p>' . $ccat->cat_name . '</p>'; $html .= '<p>' . $ccat->category_description . '</p>'; } } } } return $html; }
call it with
<?php echo list_my_categories('3,13') ?>
I just tested this and it works. Also you can use this outside of the loop. sorry
Thanks Keesiemeijer! Works like a charm!
Glad you got it working!
- The topic ‘How to create a “category loop”?’ is closed to new replies.