PHP: Get WordPress Post Categories

By  on  

When I put together my WordPress theme, I wanted to put all of my post categories in the footer of the page and in two column format. Obviously there isn't a built-in WordPress function that will format the categories the way I wanted to so it was up to me to do all the work. Here's how I was a able to put my categories in two column format.

The PHP Code

<h5>Discussion Topics</h5>
<?php
	/* BUILD THE CATEGORY LIST */
	$categories = get_categories(array('type' => 'post','child_of' => 0,'orderby' => 'name','order' => 'ASC','hide_empty' => true));
	$category_count = 0; $category_col = 1;
	
	foreach($categories as $catty) 
	{
		$category_count++;
		${'category_list_'.$category_col}[] = array('name'=>$catty->cat_name,'url'=>$catty->category_nicename);
		if(floor($category_count) == (count($categories) / 2)) { $category_col = 2; }
	}
?>
<div id="col-1">
	<?php foreach($category_list_1 as $item) { echo '<a href="/sugar/',$item['url'],'">',$item['name'],'</a>'; } ?>
</div>
<div id="col-2">
	<?php foreach($category_list_2 as $item) { echo '<a href="/sugar/',$item['url'],'">',$item['name'],'</a>'; } ?>
</div>

Easy, right? WordPress is a magnificent tool with plenty of helpful functions -- the get_categories() function is only one of them.

Recent Features

  • By
    39 Shirts &#8211; Leaving Mozilla

    In 2001 I had just graduated from a small town high school and headed off to a small town college. I found myself in the quaint computer lab where the substandard computers featured two browsers: Internet Explorer and Mozilla. It was this lab where I fell...

  • By
    5 More HTML5 APIs You Didn&#8217;t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

  • By
    MooTools CountDown Plugin

    There are numerous websites around the internet, RapidShare for example, that make you wait an allotted amount of time before presenting you with your reward. Using MooTools, I've created a CountDown plugin that allows you to easily implement a similar system. The MooTools JavaScript The CountDown class...

Discussion

  1. THANK YOU SO MUCH FOR THIS CODE!

    I had to do a little modifying to it to do what I wanted to do but your code is the basis for my XML Toolbar Menu Code!
    (http://conduit.com/)

    This now allows me to add an automatically updating WordPress Category List organized in Menus and Submenus to my toolbar!

    You probably have no idea what I’m talking about but whatever…

    THANK YOU!

  2. I have permalinks set up.
    Before permalinks setup I have query string like this,

    http://localhost/wordpress/products-page?category=2&product_id=11

    And by changing it to permalinks to numeric,

    It shows ;
    http://localhost/wordpress/products-page/po-campo1/product-3

    So, how can I get category_id and product_id at the top of the page?

    If you know that please share it with me, I really need it.

  3. conualfy

    I have one question: how can one get the categories in a tree/branches structure? I mean parents -> children -> subchildren, etc.?

  4. nice tips and good information.thank you.i like ur site .visit mine and give me some tips dude.

  5. Nice tip David. I wanted this functionality for my blog.

  6. thansk ! i neeed this code to fetch all the post in a specia category !

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!