How to give your WordPress blog categories a custom order without any plugin

Today I want to show you how I solved a problem I had with my WordPress blog just by adding a couple of lines of code.

I wanted to show only some categories on the header, as you can clearly see I only show these ones: Game Design, Monetize, Javascript, HTML5, Phaser, Box2D, ActionScript 3, Flash and I don’t show, WordPress and Php which also are blog categories, as you can see in this post.

Well, that was simple, I only had to edit the theme and add

get_categories(array("include"=>"5,6,17,22,23,27,42,44");

using get_categories you can select only some categories to show, but there’s a big missing feature in my opinion: you can’t decide in which order to display.

Default order is alphabetical order, then you only have a few more options like the number of posts, but you can’t give your categories a real custom order.

Obviously a lot of plugins come out to let bloggers give custom order to their categories, some of them written from scratch, some others starting from Advance Custom Fields plugin.

To keep my theme light and clean, I prefer not to use any plugin for easy tasks like this: let’s see how I solved this problem:

$category_order = array(22,17,6,42,44,27,23,5);
$category_array = array();
$categories=get_categories(array("include"=>"5,6,17,22,23,27,42,44"));
if ($categories) {
	foreach($categories as $category) {
		$category_array[array_search($category->cat_ID,$category_order)] = '<div class = "maincats"><a href = "/category/'.$category->category_nicename.'">' . $category->name."<br />".$category->count."</a></div>";
	}
	ksort($category_array);
	foreach($category_array as $category){
		echo $category;  	
	}
}

Line 1: this is an array containing the actual order I want to give to my categories. These are the IDs of each category.

Line 2: this is the array which will contain the HTML of the sorted categories.

Line 3: let’s get the categories!

Line 4: just checking if categories exist, in case you mispelled categories IDs.

Line 5: looping trough all categories.

Line 6: placing in the final categories array the HTML for each category, in the proper index according to the order I set at line 1.

Line 8: sorting the final categories array, by key.

Line 9: looping trough the final categories array.

Line 10: finally writing the HTML of each category in the right order.

And that’s it, I had my categories ordered by my custom order. Easy and nice, without any plugin.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers