Skip to content

Instantly share code, notes, and snippets.

@marcosfreitas
Last active November 10, 2015 20:04
Show Gist options
  • Save marcosfreitas/5db7cdacb398571968c9 to your computer and use it in GitHub Desktop.
Save marcosfreitas/5db7cdacb398571968c9 to your computer and use it in GitHub Desktop.
WORDPRESS: Retrieve children of a parent category - get_categories()
<?php
/* Subcategories of "Journals" (id: 6) */
$categories = get_categories('child_of=6&hide_empty=0');
foreach ($categories as $cat):
/* This get the total of posts in current category of the loop.*/
$total_of_posts_in_category = (int) $cat->count;
$current_category_name = $cat->name;
/* For each category, make the loop for the amount of posts */
/* New wordpress query object - passing the arguments */
$journals_query = new WP_Query('category_name='.$current_category_name.'&show_posts=-1&order_by=name&order=asc');
/* This is the loop for the current category retrieved */
while($journals_query->have_posts()):
$journals_query->the_post();
// [custom code...]
endwhile;
endforeach;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment