Tag Archive for 'get_categories'

How to list latest posts and subcategories of each top level categories in WordPress

Sorry for the long and confusing title. Do allow me to illustrate and describe what I have in mind.

What we’re going to do here is for every top-level categories, we will:

Sample custom WordPress categories style - Just CSS

  1. display the category title in bold and the category description.
  2. list the latest post title prominently displayed as the first entry and short excerpt.
  3. list is followed by four or five post titles sorted by date.
  4. finally all children/subcategories of the category listed at the bottom.

Something like what we have here on the right illustration. Using WordPress, I’ve always wanted to replicate the myriad of ways most news and magazine-style websites present its contents.

If you are familiar with wp_list_categories(), which is the one of main category tags that handles how categories are formatted and displayed, you’ll realise wp_list_categories() alone is not enough. We’ll also use get_categories(), get_cat_ID()and a wpdb query direct into WordPress’s database.

Also, I think the coding in this article is more suited for WordPress themers who prefer to hard-code it into their themes. The solution is a very straightforward sequential code where you can just copy and paste it into your sidebar.php (or wherever you wish) for instance.

Let’s get down to the source codes …

Continue reading ‘How to list latest posts and subcategories of each top level categories in WordPress’